While not a direct answer to your problem, per your request here's some info and a code example on using
.
It's generally accepted best practice to use using
statements with objects that implement IDisposable
- it essentially does the same thing as a try/finally block.
Once the scope of the using statement is exited (either the operation completes or an exception is thrown), Dispose
will be called on the object. See using Statement for more details.
Here's an abbreviated example based on your code:
int catCount = 1;
string connectionString = "server=server;user id=user;password=password;database=db;"
string query = "SELECT email FROM oc_newsletter_old_system WHERE cat_uid=@cat_uid";
while (catCount < 13)
{
using (MySqlConnection con = new MySqlConnection(connectionString))
{
con.Open();
MySqlCommand mC = new MySqlCommand(query, con);
mC.Parameters.AddWithValue("@cat_uid", catCount);
using (MySqlDataReader Rd = mC.ExecuteReader())
{
while (Rd.Read())
{
// rest of your code here
}
}
}
}
Note that the above is based on the .NET SqlConnection, SqlCommand and SqlDataReader classes, but should be the same (or very similar) for MySql.
I moved the connection string and query string to their own variables (but that's a personal preference). I also gave an example of how to do parameterized queries (the mCParameters.AddWithValue
line - getting in the habit of using parmaeterized queries is (IMO) a good thing, especially if you move from WinForms to web-based apps, as it prevents SQL Injection attacks.