public interface IOleDbDataGateway
{
void ExecuteNonQuery(string sql, params object[] args);
object ExecuteScalar(string sql, params object[] args);
DataTable FillDataTable(string sql, params object[] args);
}
public class OleDbDataGateway : IOleDbDataGateway
{
private readonly string connectionString;
public OleDbDataGateway(string connectionString)
{
this.connectionString = connectionString;
}
public void ExecuteNonQuery(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var command = new OleDbCommand(sql, connection);
connection.Open();
try
{
command.ExecuteNonQuery();
}
finally
{
connection.Close();
}
}
public object ExecuteScalar(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var command = new OleDbCommand(sql, connection);
connection.Open();
try
{
return command.ExecuteScalar();
}
finally
{
connection.Close();
}
}
public DataTable FillDataTable(string sql, params object[] args)
{
if (args != null)
{
sql = string.Format(sql, args);
}
var connection = new OleDbConnection(connectionString);
var adapter = new OleDbDataAdapter(sql, connection);
var table = new DataTable();
connection.Open();
try
{
adapter.Fill(table);
}
finally
{
connection.Close();
}
return table;
}
}