Link to home
Start Free TrialLog in
Avatar of dynamicweb09
dynamicweb09Flag for India

asked on

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.
Avatar of igordevelop
igordevelop
Flag of North Macedonia image

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
Avatar of dynamicweb09

ASKER

Thanks Igor for your reply.

Actually I want to run same procedure with different parameter  parallel y. please help me
SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Can I call Asynchronously? if yes then how?
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.
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@kaufmed

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.
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?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial