最近做了一些关于Access数据库的应用开发,其中涉及了对Access数据库的访问,现在将我们自己用到的几个基类贴出来给
大家看看,由于涉及功能并不多,其中有些也是借鉴了他人的代码,功能也比较简单,大家不要见笑了,呵呵
/// <summary>
/// 数据库操作抽象
/// </summary>
public interface IDataAccess
{
/// <summary>
/// 打开
/// </summary>
void Open();
/// <summary>
/// 关闭
/// </summary>
void Close();
/// <summary>
/// 执行 增 删 改 查
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="queryParameter">参数</param>
int ExecuteNonQuery(string sql, QueryParameter[] queryParameter);
/// <summary>
/// 查询某一个值
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>Object</returns>
object GetScalar(string sql, QueryParameter[] parameters);
/// <summary>
/// 查询多条条数据
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>DataTable</returns>
DataTable GetTable(string sql, QueryParameter[] parameters);
/// <summary>
/// 查询多条值
/// </summary>
/// <param name="sql">Sql语句</param>
/// <param name="parameters">参数</param>
/// <returns>DataReader</returns>
IDataReader GetReader(string sql, QueryParameter[] parameters);
/// <summary>
/// 开始事务
/// </summary>
void BeginTran();
/// <summary>
/// 提交事务
/// </summary>
void CommitTran();
/// <summary>
/// 回滚事务
/// </summary>
void RollBackTran();
}
public class QueryParameter
{
public string Name
{
get;
set;
}
public object Value
{
get;
set;
}
public DbType DbType
{
get;
set;
}
private ParameterDirection _driection = ParameterDirection.Input;
public ParameterDirection Driection
{
get { return _driection; }
set { _driection = value; }
}
public QueryParameter(string name, object value, DbType dbType)
{
this.DbType = dbType;
this.Value = value;
this.Name = name;
}
}
public class AccessDalHelper : IDataAccess
{
private readonly OleDbConnection _connection;
private OleDbTransaction _tran;
public AccessDalHelper(string constr)
{
_connection = new OleDbConnection(constr);
}
public void Open()
{
if (this._connection == null || this._connection.State == ConnectionState.Closed)
{
try
{
_connection.Open();
}
catch (ArgumentException)
{
throw;
}
}
}
public void Close()
{
if (this._connection != null || _tran != null)
{
_connection.Close();
}
}
public int ExecuteNonQuery(string sql, QueryParameter[] queryParameter)
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommond(cmd, CommandType.Text, queryParameter, sql);
Open();
int i = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
Close();
return i;
}
public object GetScalar(string sql, QueryParameter[] parameters)
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommond(cmd, CommandType.Text, parameters, sql);
Open();
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
Close();
return obj;
}
public System.Data.DataTable GetTable(string sql, QueryParameter[] parameters)
{
DataTable dt = new DataTable();
OleDbCommand cmd = new OleDbCommand();
PrepareCommond(cmd, CommandType.Text, parameters, sql);
Open();
OleDbDataAdapter sda = new OleDbDataAdapter(cmd);
sda.Fill(dt);
cmd.Parameters.Clear();
Close();
return dt;
}
public System.Data.IDataReader GetReader(string sql, QueryParameter[] parameters)
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommond(cmd, CommandType.Text, parameters, sql);
Open();
OleDbDataReader dr = cmd.ExecuteReader();
cmd.Parameters.Clear();
Close();
return dr;
}
public void BeginTran()
{
this._tran = this._connection.BeginTransaction();
}
public void CommitTran()
{
this._tran.Commit();
}
public void RollBackTran()
{
this._tran.Rollback();
}
private void PrepareCommond(OleDbCommand cmd, CommandType commandType, QueryParameter[] parameters, string commandtext)
{
cmd.CommandType = commandType;
cmd.CommandText = commandtext;
cmd.Connection = _connection;
cmd.Transaction = this._tran;
if (parameters != null && parameters.Length > 0)
{
for (int i = 0; i < parameters.Length; i++)
{
cmd.Parameters.AddWithValue(parameters[i].Name, parameters[i].Value);
}
}
}
}