Thursday, February 16, 2012

Accessing SQL Server 2005 from C# using multithreaded application

I have a C# program that access SQL Server 2005. The program have 4 threads that performs the same method and are used only to improve performance of the system

The C# accesses the SQL Server using OleDbConnection and

OleDbCommand classes. Sometimes (very rare) I get exception to method ExecuteNonQuery() with message: "Table does not exist.".

The command text for the query is a constant string in the format: "insert into my_table…".

Can any one tell me what could be the problem?

Please give more information regarding the following questions:

a. Are these 4 threads sharing the same SqlConnection?

b. Is the table my_table created before you start these 4 threads? Or are they created inside one or more of these threads?

c. What else do you run in the threads apart from the INSERT INTO my_table?

d. Do you Open and Close the SqlConnection in these threads?

One possibility that I can think of is that the SqlCommand maybe running under a different database context, hence the server cannot find the table you specified. But I need to understand your application better in order to find out the exact reason.

Ping

|||

Thanks for your response!!

Here are the answers:

a. Are these 4 threads sharing the same SqlConnection?

Answer: Each thread work on its own connection, i.e. we have 4 connections.

b. Is the table my_table created before you start these 4 threads? Or are they created inside one or more of these threads?

Answer: The table is created before the threads are started.

c. What else do you run in the threads apart from the INSERT INTO my_table?

Answer: Each thread make several inserts to the same table and also execute a store procedure (the same one with different parameters).

d. Do you Open and Close the SqlConnection in these threads?

Answer: Each thread open a connection, make serveral inserts and execute a store procedure and then close the connection and terminates. Whenever the thread is terminated another thread is started until all the work is done.

|||

Thanks for your response!!

Here are the answers:

a. Are these 4 threads sharing the same SqlConnection?

Answer: Each thread work on its own connection, i.e. we have 4 connections.

b. Is the table my_table created before you start these 4 threads? Or are they created inside one or more of these threads?

Answer: The table is created before the threads are started.

c. What else do you run in the threads apart from the INSERT INTO my_table?

Answer: Each thread make several inserts to the same table and also execute a store procedure (the same one with different parameters).

d. Do you Open and Close the SqlConnection in these threads?

Answer: Each thread open a connection, make serveral inserts and execute a store procedure and then close the connection and terminates. Whenever the thread is terminated another thread is started until all the work is done.


|||

Hi,

What you could do is to capture a detailed SQL Profiler trace and examine the statements around the error occurrence. This would give you an idea why the ExecuteNonQuery is getting the non-exist error.

HTH,
Jivko Dobrev - MSFT
--
This posting is provided "AS IS" with no warranties, and confers no rights.

|||

i have a very weird thing going on in my application program..

you see..

my c# application program has an sql connection..

and it can save records..

but as soon as i stop debigging it

and then re-run it again..

all the records saved in my Database

literally disappears.!!

i need help..

please help out a young kid here...T_T

No comments:

Post a Comment