I'm writing a Database class using c# that interacts with a multithread sdk. I should use only one connection(No need to say!) However, I always get errors about connection and datareaders. Anly help appreciated.
Here is the structure of my code
static class Db
{
static MySqlConnection conn = new MySqlConnection(connectionString);
private static void Connect()
{
if (conn.State == ConnectionState.Closed)
conn.Open();
}
private static void DisConnect()
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
static int Insert()
{
Connect();
MySqlCommand cmd = new MySqlCommand(sql, conn);
cmd.ExecuteNonQuery();
return Convert.ToInt32(cmd.LastInsertedId);
DisConnect();
}
public static DataTable select(string sql) //Especially fails here
{
Connect();
MySqlCommand cmd = new MySqlCommand(sql, conn);
using (MySqlDataReader read = cmd.ExecuteReader())
{
if (read.HasRows){ Some Code... }
}
DisConnect();
}
}