how to use multiple instance of a db connection with multiple thread in C#.Net
I have a console application(C#). I want to use more than one instance of mysqlconnection.
I have a master table with 50000 rows. I want to split the data in 10 set and want to run some procedures parallel with the data. please help me.
.NET ProgrammingMySQL ServerC#
Last Comment
Jacques Bourgeois (James Burger)
8/22/2022 - Mon
igordevelop
Hi,
Using multiple instances is usually something that developers want to avoid. In fact, you should use the same instance for multiple operations. Make the connection instance to be global so you can access it from any code-block and just play with its state (open it and close it after use each time).
Example, make this global, public:
SqlConnection conn = new SqlConnection("ConnectionString");
Then, every time you need to use this connection don't forget to open and close it, or if you use it for parallel execution do not close it after all queries are executed;
conn.Open();
//run queries
conn.Close();
Let me know if anything.
Regards,
Igor
dynamicweb09
ASKER
Thanks Igor for your reply.
Actually I want to run same procedure with different parameter parallel y. please help me
Once again, I speak of my experience with SQL Server, so I am not sure that MySql has those, but the Command object has a series of BeginExecute methods that runs asynchronously. Since all the ADO.NET libraries follow the same model, you might have those on your side.
As stated in your reference, MARS works through "Interleaved execution". It is my understanding and my experience that from the application point of view, you feel that the result is the same as if the stuff was working in real parallel execution. If you start a long asynchrone query, and follow it with a short asynchrone query, the second one will finish before the first.
kaufmed
I don't believe that is guaranteed, though. The reference states "at well-defined points." Do you know what those "well-defined points" are? Is it guaranteed that a short query will finish before a long query?
Using multiple instances is usually something that developers want to avoid. In fact, you should use the same instance for multiple operations. Make the connection instance to be global so you can access it from any code-block and just play with its state (open it and close it after use each time).
Example, make this global, public:
SqlConnection conn = new SqlConnection("ConnectionS
Then, every time you need to use this connection don't forget to open and close it, or if you use it for parallel execution do not close it after all queries are executed;
conn.Open();
//run queries
conn.Close();
Let me know if anything.
Regards,
Igor