1

I have a mobile application that connects directly to an instance of SQL Server 2008.

Normally, if this was was a desktop application I would wrap every database call in a using statement:

using (SqlConnection sqlConnection = new SqlConnection(ConnectionString))
{ }

And if it was a mobile application connecting to a local compact database, I would leave the connection open for the life of the application (since it has to be rebuilt every time).

But, now I'm faced with a mobile application that's connecting to a real server...so would it be best to follow the desktop route, or the mobile route? At first I was going to do the desktop route but then I remembered that pooling isn't even supported on the mobile version so maybe it would be best to leave it open?

JWrightII
  • 942
  • 11
  • 26

1 Answers1

2

Mobile devices are wireless - battery operated.

They are designed to use very little power by shutting down resources when not in use. That goes for your wireless radio!

It will turn back on when you need it, but an Open connection will not know that.

My guess is you are going to run into connection issues with that. The software will make one call and wait for a response.

If the wireless radio was not connected at the time, it may or may not throw an error for you to see.

Whether it throws an error or not, how do you go about re-establishing a connection with your application? Unless you write code in there, you would need to restart your app.

I do not think there is any RIGHT or WRONG way to do this, but I would think opening the connection only when you needed it would be a BEST PRACTICE idea.

  • I think I would have to agree. I do think it will be a little bit slower, but always having to attempt a connection before making a call will make it easier to handle situations where the device has wandered out of range or the SQL server has become unavailable. Will wait a big longer in case someone else can offer up anything I'm not considering, but if I don't get any other answers I'll mark this one. – JWrightII May 08 '13 at 14:15
  • jp2code is right, you cannot assume a mobile device is always connected. You have to wtahc the connection and if it has been disconnected you have to re-establish it. Do you really need the full always connected access? You may consider keeping a local 'view' of the database using RDA (http://msdn.microsoft.com/en-us/library/aa257442%28v=sql.80%29.aspx) and Merge Replication. So you are able to pull data and push updates. Paul Yao has written a nice chapter about RDA and Merge Replication in "Programming the Compact Framework". – josef May 08 '13 at 16:42