question about mysql and c#
#3

While coding the emulator i noticed that in case of a exception the mysql connection doesnt close in a try catch block.

And eventually will flood the connection pool.

I suggest to work like this

 

using(MySqlConnection dbConnerction = new MySqlConnection(strConnection))
{
dbConnerction.Open();

//executing a query
string query = "SELECT * FROM test_table";
MySqlCommand dbCommand = new MySqlCommand(query, dbConnerction);
MySqlDataReader dbReader = dbCommand.ExecuteReader();

//Reading the data
while (dbReader.Read()){
string stuff = dbReader["test_column1"].ToString();
float f= dbReader.GetFloat("test_column2");
}

}
This way everything inside the using block will always get disposed when leaving the using block.


Messages In This Thread
[No subject] - by soryjero - 07-08-2013, 01:03 PM
[No subject] - by someone - 07-08-2013, 05:23 PM
[No subject] - by Wizatek - 07-08-2013, 06:52 PM
[No subject] - by soryjero - 07-08-2013, 10:03 PM
[No subject] - by soryjero - 07-09-2013, 09:58 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)