数据访问基础类(基于Access数据库)
数据访问基础类(基于Access数据库)
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/// <summary>
/// 数据访问基础类(基于Access数据库)
/// </summary>
public class OleDbHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
/// <summary>
/// 默认构造函数;
/// </summary>
public OleDbHelper()
{
}
#region 执行简单SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务;
/// </summary>
/// <param name="SQLStringList">多条SQL语句;</param>
///<returns>成功返回true,否则返回false;</returns>
public bool ExecuteSqlTransaction(ArrayList SQLStringList)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
connection.Open();
cmd.Connection = connection;
OleDbTransaction tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
return true;
}
catch (OleDbException ex)
{
tx.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetScalar(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行查询语句,返回OleDbDataReader,需要自己关闭DataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>OleDbDataReader</returns>
public OleDbDataReader GetReader(string strSQL)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
try
{
connection.Open();
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(SQLString, connection);
adapter.Fill(ds, "ds");
connection.Close();
return ds;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
#endregion
#region 执行带参数的SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key是该语句的SqlParameter[],value为sql语句)</param>
///<returns >如果成功返回true,否则返回false;</returns>
public bool ExecuteSqlTransaction(Hashtable SQLStringList)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbTransaction trans = connection.BeginTransaction())
{
OleDbCommand cmd = new OleDbCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
OleDbParameter[] cmdParms = (OleDbParameter[])myDE.Key;
PrepareCommand(cmd, connection, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(OleDbException ex)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>查询结果(object)</returns>
public object GetScalar(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行查询语句,返回OleDbDataReader
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>OleDbDataReader</returns>
public OleDbDataReader GetReader(string SQLString, DbParameter[] cmdParms)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 初始Command
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">数据库链接;</param>
/// <param name="trans">事务对象</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="cmdParms">参数</param>
private void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
#endregion
}
}
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using System.Data.Common;
namespace DataBase
{
/// <summary>
/// 数据访问基础类(基于Access数据库)
/// </summary>
public class OleDbHelper
{
protected static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
/// <summary>
/// 默认构造函数;
/// </summary>
public OleDbHelper()
{
}
#region 执行简单SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务;
/// </summary>
/// <param name="SQLStringList">多条SQL语句;</param>
///<returns>成功返回true,否则返回false;</returns>
public bool ExecuteSqlTransaction(ArrayList SQLStringList)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
connection.Open();
cmd.Connection = connection;
OleDbTransaction tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
return true;
}
catch (OleDbException ex)
{
tx.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <returns>查询结果(object)</returns>
public object GetScalar(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行查询语句,返回OleDbDataReader,需要自己关闭DataReader
/// </summary>
/// <param name="strSQL">查询语句</param>
/// <returns>OleDbDataReader</returns>
public OleDbDataReader GetReader(string strSQL)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand(strSQL, connection);
try
{
connection.Open();
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return reader;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(SQLString, connection);
adapter.Fill(ds, "ds");
connection.Close();
return ds;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
#endregion
#region 执行带参数的SQL语句
/// <summary>
/// 执行SQL语句,返回影响的记录数
/// </summary>
/// <param name="SQLString">SQL语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>影响的记录数</returns>
public int ExecuteSql(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行多条SQL语句,实现数据库事务。
/// </summary>
/// <param name="SQLStringList">SQL语句的哈希表(key是该语句的SqlParameter[],value为sql语句)</param>
///<returns >如果成功返回true,否则返回false;</returns>
public bool ExecuteSqlTransaction(Hashtable SQLStringList)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
connection.Open();
using (OleDbTransaction trans = connection.BeginTransaction())
{
OleDbCommand cmd = new OleDbCommand();
try
{
//循环
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Value.ToString();
OleDbParameter[] cmdParms = (OleDbParameter[])myDE.Key;
PrepareCommand(cmd, connection, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
return true;
}
catch(OleDbException ex)
{
trans.Rollback();
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object),返回首行首列的值;
/// </summary>
/// <param name="SQLString">计算查询结果语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>查询结果(object)</returns>
public object GetScalar(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 执行查询语句,返回OleDbDataReader
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>OleDbDataReader</returns>
public OleDbDataReader GetReader(string SQLString, DbParameter[] cmdParms)
{
OleDbConnection connection = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
/// <summary>
/// 执行查询语句,返回DataSet
/// </summary>
/// <param name="SQLString">查询语句</param>
/// <param name="cmdParms">参数列表</param>
/// <returns>DataSet</returns>
public DataSet GetDataSet(string SQLString, DbParameter[] cmdParms)
{
using (OleDbConnection connection = new OleDbConnection(connectionString))
{
OleDbCommand cmd = new OleDbCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
return ds;
}
catch (OleDbException ex)
{
connection.Close();
connection.Dispose();
log4net.ILog log = log4net.LogManager.GetLogger("File");
log.Debug(ex);
throw ex;
}
}
}
}
/// <summary>
/// 初始Command
/// </summary>
/// <param name="cmd">Command对象</param>
/// <param name="conn">数据库链接;</param>
/// <param name="trans">事务对象</param>
/// <param name="cmdText">SQL语句</param>
/// <param name="cmdParms">参数</param>
private void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, DbParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (OleDbParameter parm in cmdParms)
{
cmd.Parameters.Add(parm);
}
}
}
#endregion
}
}