public class MySQLGear : IDisposable
{
private readonly MySqlConnection _connection;
public MySQLGear(string connectionString)
{
_connection = new MySqlConnection(connectionString);
}
protected MySqlConnection Connection()
{
if (_connection == null)
{
throw new ObjectDisposedException("The underlying connection has been closed");
}
if (_connection.State != ConnectionState.Open)
{
_connection.Open();
}
return _connection;
}
public MySqlDataReader GetReader(string queryMysql)
{
using (var cmdMysql = new MySqlCommand(queryMysql, Connection());)
{
return cmdMysql.ExecuteReader();
}
}
public int MakeQuery(string queryMysql)
{
using (var cmdMysql = new MySqlCommand(queryMysql, Connection());)
{
return cmdMysql.ExecuteNonQuery();
}
}
public object MakeScalar(string queryMysql)
{
using (var cmdMysql = new MySqlCommand(queryMysql, Connection());)
{
return cmdMysql.ExecuteScalar();
}
}
~MySQLGear()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (_connection != null)
{
if (_connection.State != ConnectionState.Closed)
{
_connection.Dispose();
}
_connection = null;
}
}
}
Peter Kiss
- 2.4k
- 1
- 13
- 17