OleDbHelper.cs
上传用户:simon2hong
上传日期:2021-11-18
资源大小:16746k
文件大小:10k
- using System;
- using System.Data;
- using System.Data.Common;
- using System.Text;
- using System.Data.OleDb;
- namespace DBUtility
- {
- /// <summary>
- /// 数据库操作基类(for OleDb)
- /// </summary>
- internal class OleDbHelper : IDBHelper
- {
- /// <summary>
- /// 获取分页SQL
- /// </summary>
- /// <param name="tblName">表名</param>
- /// <param name="pageSize">每页显示条数</param>
- /// <param name="pageIndex">第几页</param>
- /// <param name="fldSort">排序字段(最后一个不需要填写正序还是倒序,例如:id asc, name)</param>
- /// <param name="fldDir">最后一个排序字段的正序或倒序(true为倒序,false为正序)</param>
- /// <param name="condition">条件</param>
- /// <returns>返回用于分页的SQL语句</returns>
- private string GetPagerSQL(string tblName, int pageSize, int pageIndex, string fldSort, bool fldDir, string condition)
- {
- string strDir = fldDir ? " ASC" : " DESC";
- if (pageIndex == 1)
- {
- return "select top " + pageSize.ToString() + " * from " + tblName.ToString()
- + ((string.IsNullOrEmpty(condition)) ? string.Empty : (" where " + condition))
- + " order by " + fldSort.ToString() + strDir;
- }
- else
- {
- StringBuilder strSql = new StringBuilder();
- strSql.AppendFormat("select top {0} * from {1} ", pageSize, tblName);
- strSql.AppendFormat(" where {1} not in (select top {0} {1} from {2} ", pageSize * (pageIndex - 1),
- (fldSort.Substring(fldSort.LastIndexOf(',') + 1, fldSort.Length - fldSort.LastIndexOf(',') - 1)), tblName);
- if (!string.IsNullOrEmpty(condition))
- {
- strSql.AppendFormat(" where {0} order by {1}{2}) and {0}", condition, fldSort, strDir);
- }
- else
- {
- strSql.AppendFormat(" order by {0}{1}) ", fldSort, strDir);
- }
- strSql.AppendFormat(" order by {0}{1}", fldSort, strDir);
- return strSql.ToString();
- }
- }
- /// <summary>
- /// 分页获取数据
- /// </summary>
- /// <param name="connectionString">连接字符串</param>
- /// <param name="tblName">表名</param>
- /// <param name="fldName">字段名</param>
- /// <param name="pageSize">页大小</param>
- /// <param name="pageIndex">第几页</param>
- /// <param name="fldSort">排序字段</param>
- /// <param name="fldDir">升序{False}/降序(True)</param>
- /// <param name="condition">条件(不需要where)</param>
- public DbDataReader GetPageList(string connectionString, string tblName, int pageSize,
- int pageIndex, string fldSort, bool fldDir, string condition)
- {
- string sql = GetPagerSQL(tblName, pageSize, pageIndex, fldSort, fldDir, condition);
- return ExecuteReader(connectionString, CommandType.Text, sql, null);
- }
- /// <summary>
- /// 得到数据条数
- /// </summary>
- public int GetCount(string connectionString, string tblName, string condition)
- {
- StringBuilder sql = new StringBuilder("select count(*) from " + tblName);
- if (!string.IsNullOrEmpty(condition))
- sql.Append(" where " + condition);
- object count = ExecuteScalar(connectionString, CommandType.Text, sql.ToString(), null);
- return int.Parse(count.ToString());
- }
- /// <summary>
- /// 执行查询,返回DataSet
- /// </summary>
- public DataSet ExecuteQuery(string connectionString, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- using (OleDbConnection conn = new OleDbConnection(connectionString))
- {
- using (OleDbCommand cmd = new OleDbCommand())
- {
- PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
- using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
- {
- DataSet ds = new DataSet();
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- return ds;
- }
- }
- }
- }
- /// <summary>
- /// 在事务中执行查询,返回DataSet
- /// </summary>
- public DataSet ExecuteQuery(DbTransaction trans, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
- OleDbDataAdapter da = new OleDbDataAdapter(cmd);
- DataSet ds = new DataSet();
- da.Fill(ds, "ds");
- cmd.Parameters.Clear();
- return ds;
- }
- /// <summary>
- /// 执行 Transact-SQL 语句并返回受影响的行数。
- /// </summary>
- public int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- using (OleDbConnection conn = new OleDbConnection(connectionString))
- {
- PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return val;
- }
- }
- /// <summary>
- /// 在事务中执行 Transact-SQL 语句并返回受影响的行数。
- /// </summary>
- public int ExecuteNonQuery(DbTransaction trans, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return val;
- }
- /// <summary>
- /// 执行查询,返回DataReader
- /// </summary>
- public DbDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- OleDbConnection conn = new OleDbConnection(connectionString);
- try
- {
- PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
- OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return rdr;
- }
- catch
- {
- conn.Close();
- throw;
- }
- }
- /// <summary>
- /// 在事务中执行查询,返回DataReader
- /// </summary>
- public DbDataReader ExecuteReader(DbTransaction trans, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
- OleDbDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return rdr;
- }
- /// <summary>
- /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
- /// </summary>
- public object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- using (OleDbConnection connection = new OleDbConnection(connectionString))
- {
- PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
- object val = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- return val;
- }
- }
- /// <summary>
- /// 在事务中执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。
- /// </summary>
- public object ExecuteScalar(DbTransaction trans, CommandType cmdType, string cmdText,
- params DbParameter[] cmdParms)
- {
- OleDbCommand cmd = new OleDbCommand();
- PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
- object val = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- return val;
- }
- /// <summary>
- /// 生成要执行的命令
- /// </summary>
- private static void PrepareCommand(DbCommand cmd, DbConnection conn, DbTransaction trans, CommandType cmdType,
- string cmdText, DbParameter[] cmdParms)
- {
- if (conn.State != ConnectionState.Open)
- conn.Open();
- cmd.Connection = conn;
- cmd.CommandText = cmdText.Replace("?", "@").Replace(":", "@");
- if (trans != null)
- cmd.Transaction = trans;
- cmd.CommandType = cmdType;
- if (cmdParms != null)
- {
- foreach (OleDbParameter parm in cmdParms)
- {
- parm.ParameterName = parm.ParameterName.Replace("?", "@").Replace(":", "@");
- cmd.Parameters.Add(parm);
- }
- }
- }
- }
- }