I've developed my own class in ASP.NET project to access MySQL and make queries and scalars, and read the results from it.
I want you to review my class and tell me where I've made a mistake.
Here are some questions that are important for me:
- Do I use a reference correctly for the connector class of MySQL in methods?
- Is it correct to make it NON-static class as it is? For the GC, memory reasons?
- Do I need to add some events, delegate for the autoclose for the MySQL connection, or is there no need in it, because out of scope it will be auto freed from memory?
public class MySQLGear
{
private const string connStr = "Server = localhost; Database = self; Uid = someuser; Pwd = 1234;";
public MySqlConnection CreateConnection()
{
MySqlConnection connMysql = new MySqlConnection(connStr);
connMysql.Open();
if (connMysql.State == ConnectionState.Open)
{
return connMysql;
}
else return null;
}
public MySqlDataReader GetReader(ref MySqlConnection connMysql, string queryMysql)
{
MySqlCommand cmdMysql = new MySqlCommand(queryMysql, connMysql);
MySqlDataReader readerMysql = cmdMysql.ExecuteReader();
return readerMysql;
}
public int MakeQuery(ref MySqlConnection connMysql, string queryMysql)
{
MySqlCommand cmdMysql = new MySqlCommand(queryMysql, connMysql);
int result = cmdMysql.ExecuteNonQuery();
return result;
}
public object MakeScalar(ref MySqlConnection connMysql, string queryMysql)
{
MySqlCommand cmdMysql = new MySqlCommand(queryMysql, connMysql);
object result = cmdMysql.ExecuteScalar();
return result;
}
}