Click here to Skip to main content
16,012,061 members
Home / Discussions / C#
   

C#

 
AnswerRe: About grid checkBox Pin
stancrm16-May-08 0:52
stancrm16-May-08 0:52 
GeneralRe: About grid checkBox Pin
Prabhat00316-May-08 1:03
Prabhat00316-May-08 1:03 
QuestionC# MS Sql Express: share connection between threads? Pin
Michiel15-May-08 23:46
Michiel15-May-08 23:46 
AnswerRe: C# MS Sql Express: share connection between threads? Pin
originSH16-May-08 0:34
originSH16-May-08 0:34 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
Michiel16-May-08 1:20
Michiel16-May-08 1:20 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
originSH16-May-08 1:28
originSH16-May-08 1:28 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
Michiel16-May-08 3:48
Michiel16-May-08 3:48 
AnswerRe: C# MS Sql Express: share connection between threads? Pin
The Nightcoder16-May-08 13:32
The Nightcoder16-May-08 13:32 
If I remember correctly, the current SQL Native Client (preferred when talking to SQL Server 2005) doesn't support connection pooling. This means you get a performance hit if you solve your problems by creating, opening and closing a separate connection each time you touch the database (each time you will cause negotiations between the client and SQL Server before you start doing actual work). If connection pooling was supported, this wouldn't be the case, meaning it would be the simplest solution and wouldn't be a performance issue (connection pooling makes it extremely fast to open/close new connections as a number of "closed" connections are kept open in the background, ready to use by the next attempt to open a new connection).

As is, it isn't always workable (depending on how often you touch the database, of course - if it's just once an hour you should of course connect and close each time).

My solution when doing what you're doing (I have a couple of apps that have similar structure) is as follows:

- One thread for listening to the TCP socket and accessing the database accordingly: Has its own SQL Server connection, opened when the thread starts and kept as long as it runs.

- One thread (the main UI thread, usually) that reacts to user interaction and accesses the database accordingly: Has its own SQL Server connection, opened when the application starts and kept as long as it runs.

- One thread that loops with a Thread.Sleep with some appropriate interval and checks the database for stuff to do: Has its own SQL Server connection, opened when the thread starts and kept as long as it runs. NOTE: If the checks and actions aren't time-consuming you could do them from a Timer object in the UI, using the connection of the UI thread (as the Windows Timer object lives on the UI thread and won't fire when some other UI event handler is running).

If your application is meant to stay running for long times and don't need to touch the database frequently, you could wrap your database code in a class that opens the actual connection on demand and closes it after some time of inactivity (purpose: minimize the number of open connections to the SQL Server). But if I understand your scenario correctly (reading between the lines), this isn't the case.

Finally, a short warning: Beware of classes that retain a reference to the connection and use it unexpectedly. You need to make sure that each distinct action on the database doesn't leave any SqlCommand, SqlDataReader or similar objects laying around (using blocks are Good Things). For instance, don't create a reader in the TCP socket listener thread and store it for use by the UI thread - you're bound to run into threading issues when the UI thread starts reading from it (that particular example is actually even worse than that - the reader will block the connection until it is closed, unless you have MARS enabled). Always consume your data (read it into variables, a disconnected DataTable or whatever) immediately. If you can't due to large volumes of data, you have to create a brand new connection for each such action.

Note: Reading your error messages, I think you may have the SqlDataReader issue. As long as one of them is open and alive, you can't do anything else on the same connection (not even on the same thread). Unless (which has already been said by others) you enable multiple result sets (MARS) - but that won't solve any other threading issues.

Was this helpful?

--
Peter

GeneralRe: C# MS Sql Express: share connection between threads? Pin
originSH18-May-08 22:10
originSH18-May-08 22:10 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
The Nightcoder18-May-08 23:28
The Nightcoder18-May-08 23:28 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
originSH18-May-08 23:56
originSH18-May-08 23:56 
GeneralRe: C# MS Sql Express: share connection between threads? [modified] Pin
Michiel20-May-08 23:44
Michiel20-May-08 23:44 
GeneralRe: C# MS Sql Express: share connection between threads? [modified] Pin
The Nightcoder21-May-08 2:58
The Nightcoder21-May-08 2:58 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
Michiel21-May-08 3:33
Michiel21-May-08 3:33 
GeneralRe: C# MS Sql Express: share connection between threads? Pin
The Nightcoder21-May-08 3:43
The Nightcoder21-May-08 3:43 
AnswerRe: C# MS Sql Express: share connection between threads? Pin
The Nightcoder16-May-08 13:52
The Nightcoder16-May-08 13:52 
AnswerRe: C# MS Sql Express: share connection between threads? Pin
The Nightcoder16-May-08 14:28
The Nightcoder16-May-08 14:28 
QuestionDrawEllipse and FillEllipse methods Pin
NarVish15-May-08 23:11
NarVish15-May-08 23:11 
AnswerRe: DrawEllipse and FillEllipse methods Pin
Christian Graus15-May-08 23:28
protectorChristian Graus15-May-08 23:28 
AnswerRe: DrawEllipse and FillEllipse methods Pin
John_Adams15-May-08 23:32
John_Adams15-May-08 23:32 
AnswerRe: DrawEllipse and FillEllipse methods Pin
CPallini16-May-08 4:09
mveCPallini16-May-08 4:09 
Questioninternal protected problem Pin
Pankaj Garg15-May-08 23:11
Pankaj Garg15-May-08 23:11 
AnswerRe: internal protected problem Pin
Vikram A Punathambekar16-May-08 0:09
Vikram A Punathambekar16-May-08 0:09 
Questionthread safety using Interlocked Pin
George_George15-May-08 23:09
George_George15-May-08 23:09 
AnswerRe: thread safety using Interlocked Pin
Guffa16-May-08 1:39
Guffa16-May-08 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.