Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I'm writing an application which uses a connection to a mdf sql db.
I use this using a SqlConnection object.
Since the db is a local one which no-one can access but me, can I keep the connection opened from the application starting to the ending?

Thanks a lot!

Jymmy097
Posted
Comments
[no name] 9-May-14 16:09pm    
Sure you could. It's not a good idea to get into developing bad habits but you can do whatever you want.
LLLLGGGG 9-May-14 16:14pm    
I know that's a bad habit, but I don't want to close the connection and reopen it.are there performance issues opening and closing a connection? Because I fear there are... That's why I didn't want to close it.
Is it a good argument as I'm a newbie in this field?
[no name] 9-May-14 16:28pm    
No it is not a good argument :-) You are more likely to notice a "performance issue" from your queries, not from opening and closing the connection. Opening and closing the connection is probably the least of your worries.
LLLLGGGG 9-May-14 16:49pm    
Ok, thanks a lot!! Sorry for the stupid question, but It's only some day since I've started to learn Sql and Connections.
[no name] 9-May-14 16:52pm    
Not a problem at all. As a side note, opening the connection has been optimized for performance using a connection pool. Read more about it, http://msdn.microsoft.com/en-us/library/8xx3tyca(v=vs.110).aspx

1 solution

You can, as Wes has said. But... It isn't a good idea.
If you are using it locally only, then probably you shouldn't be using it via SQL - ACE/ODBC is probably a better idea, because it means that you have to change the code in order to convert it to a multiuser system. Using SQL for single user and coding for a single user is dangerous, because in 3 months time you will have forgotten that when you release the software to a dozen other users and sit back in pride watching the whole system fall over from time to time... :laugh:

It's poor practice for a reason: because they can cause problems later on when it's a lot, lot more expensive to find and fix them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900