平时使用的数据库操作类整理更新后备份,记录下来以供以后使用,并顺便分享给大家一起交流。直接上源码:
整个底层操作方法分为4个类,基础方法类:SqlBase 基础工具类:SqlTool 日志类:DbLog 和MSSQL操作类:MsSqlHelper。
由于平时工作未用到其他类型数据库,因此未整理其他数据库的操作类,以后用到的话会进行更新。
首先是通用的数据库底层操作方法整理:
/// <summary> /// 数据库操作基础方法类 /// </summary> public class SqlBase { #region 字段与属性声明 private static int _cmdTimeOut = 30; /// <summary> /// 等待命令执行的时间(以秒为单位) /// </summary> public static int CMDTimeOut { get { return _cmdTimeOut; } set { _cmdTimeOut = value; } } private static string _defaultDb = "conn"; /// <summary> /// 默认数据库连接配置名称 /// </summary> public static string DefaultDb { get { return _defaultDb; } set { _defaultDb = value; } } /// <summary> /// 默认数据集名称 /// </summary> private static string _defaultDataSet = "ds"; /// <summary> /// 默认数据表名称 /// </summary> private static string _defaultDataTable = "dt"; /// <summary> /// 数据库连接失败返回信息 /// </summary> private static string _linkErrMsg = "数据库连接失败"; /// <summary> /// 存储过程返回值参数名 /// </summary> private static string _returnParaName = "ReturnValue"; #endregion /// <summary> /// 默认数据库连接字符串 /// </summary> public static string _connStr = String.Empty; /// <summary> /// 获取数据库连接 /// </summary> /// <param name="useDb">数据库简写</param> public static string GetConnectionString(string useDb) { switch (useDb) { case "conn": return _connStr; default: useDb = DefaultDb; break; } return System.Configuration.ConfigurationManager.AppSettings[useDb].ToString(); } /// <summary> /// 获取数据库连接 /// </summary> /// <param name="useDb">数据库简写</param> private static SqlConnection GetConnection(string useDb) { if (String.IsNullOrEmpty(useDb)) useDb = DefaultDb; var connStr = GetConnectionString(useDb); if (!String.IsNullOrEmpty(connStr)) { return new SqlConnection(connStr); } return null; } /// <summary> /// 执行数据库操作 /// </summary> /// <param name="sql">数据库执行语句</param> /// <param name="useDb">数据库简写</param> /// <returns>受到影响的行数</returns> public static int ExecuteNonQuery(string sql, string useDb = "") { return ExecuteNonQuery(sql, null, useDb); } /// <summary> /// 执行数据库操作 /// </summary> /// <param name="sql">数据库执行语句</param> /// <param name="paras">参数集</param> /// <param name="useDb">数据库简写</param> /// <returns>受影响的行数</returns> public static int ExecuteNonQuery(string sql, SqlParameter[] paras, string useDb = "") { SqlConnection conn = GetConnection(useDb); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); var cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = CMDTimeOut; cmd.Parameters.Clear(); if (paras != null) { cmd.Parameters.AddRange(paras); } return cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { if (conn != null) conn.Close(); } } /// <summary> /// 执行SQL语句并返回受影响的行数,带事务操作 /// </summary> /// <param name="trans">事务</param> /// <param name="sql">数据库执行语句</param> /// <param name="paras">参数集</param> /// <returns></returns> public static int ExecuteNonQuery(SqlTransaction trans, string sql, SqlParameter[] paras = null) { try { if (trans.Connection.State == ConnectionState.Closed) { trans.Connection.Open(); } SqlCommand command = new SqlCommand(sql, trans.Connection); command.CommandTimeout = CMDTimeOut; command.Transaction = trans; command.Parameters.Clear(); if (paras != null) { command.Parameters.AddRange(paras); } return command.ExecuteNonQuery(); } catch (Exception e) { throw e; } finally { trans.Connection.Close(); } } /// <summary> /// 执行SQL语句并返回受影响的行数,带事务操作(只针对sqlserver) /// </summary> /// <param name="sql"></param> /// <param name="paras"></param> /// <returns></returns> public static int ExecuteNonQueryWithTransation(string sql, SqlParameter[] paras = null, string useDB = "") { SqlTransaction sqlTransaction = null; SqlConnection conn = GetConnection(useDB); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); sqlTransaction = conn.BeginTransaction(); var cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = CMDTimeOut; cmd.Transaction = sqlTransaction; cmd.Parameters.Clear(); if (paras != null) { cmd.Parameters.AddRange(paras); } var ret = cmd.ExecuteNonQuery(); sqlTransaction.Commit(); return ret; } catch (System.Data.SqlClient.SqlException e) { sqlTransaction.Rollback(); throw e; } finally { if (conn != null) conn.Close(); } } /// <summary> /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。 /// </summary> /// <param name="sql">数据库执行语句</param> /// <param name="parameters">参数集</param> /// <param name="useDb">数据库简写</param> /// <returns>返回查询所返回的结果集中第一行的第一列或空引用(如果结果集为空).忽略其他列或行</returns> public static object ExecuteScalar(string sql, string useDb = "") { return ExecuteScalar(sql, null, useDb); } /// <summary> /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。 /// </summary> /// <param name="sql">计算查询结果语句</param> /// <returns>查询结果(object)</returns> public static object ExecuteScalar(string sql, SqlParameter[] paras, string useDB = "") { SqlConnection conn = GetConnection(useDB); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); var cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = CMDTimeOut; cmd.Parameters.Clear(); if (paras != null) { cmd.Parameters.AddRange(paras); } var obj = cmd.ExecuteScalar(); if (Object.Equals(obj, null) || Object.Equals(obj, System.DBNull.Value)) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { if (conn != null) conn.Close(); } } /// <summary> /// 执行查询,并返回查询所返回的结果集中第一行的第一列。忽略其他列或行。 /// </summary> /// <param name="trans">事务</param> /// <param name="sql">数据库执行语句</param> /// <param name="cmdParms">参数集</param> /// <returns></returns> public static object ExecuteScalar(SqlTransaction trans, string sql, SqlParameter[] cmdParms = null) { try { if (trans.Connection.State == ConnectionState.Closed) { trans.Connection.Open(); } SqlCommand cmd = new SqlCommand(sql, trans.Connection); cmd.CommandTimeout = CMDTimeOut; cmd.Transaction = trans; cmd.Parameters.Clear(); if (cmdParms != null) { cmd.Parameters.AddRange(cmdParms); } var obj = cmd.ExecuteScalar(); if (Object.Equals(obj, null) || Object.Equals(obj, System.DBNull.Value)) { return null; } else { return obj; } } catch (Exception e) { throw e; } finally { trans.Connection.Close(); } } /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="sql">查询语句</param> /// <param name="useDb">数据库简写</param> /// <returns>数据集</returns> public static DataSet Query(string sql, string useDb = "") { var ds = new DataSet(); SqlConnection conn = GetConnection(useDb); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = CMDTimeOut; var command = new SqlDataAdapter(cmd); command.Fill(ds, _defaultDataSet); } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { if (conn != null) conn.Close(); } return ds; } /// <summary> /// 执行查询语句,返回DataSet(带参数化) /// </summary> /// <param name="sql">sql语句</param> /// <param name="cmdParms">参数</param> /// <param name="useDb">数据库简写</param> /// <returns>数据集</returns> public static DataSet QueryWithParams(string sql, SqlParameter[] cmdParms = null, string useDb = "") { var ds = new DataSet(); SqlConnection conn = GetConnection(useDb); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn); cmd.CommandTimeout = CMDTimeOut; cmd.Parameters.Clear(); if (cmdParms != null) { cmd.Parameters.AddRange(cmdParms); } var command = new SqlDataAdapter(cmd); command.Fill(ds, _defaultDataSet); } catch (System.Data.SqlClient.SqlException e) { throw e; } finally { if (conn != null) conn.Close(); } return ds; } #region 存储过程方法 /// <summary> /// 执行存储过程 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="useDB">数据库简写</param> /// <param name="Times">超时时间</param> /// <returns>DataSet</returns> public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string useDB = "") { SqlConnection conn = GetConnection(useDB); try { if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); DataSet dataSet = new DataSet(); SqlDataAdapter sqlDA = new SqlDataAdapter(); sqlDA.SelectCommand = BuildQueryCommand(conn, storedProcName, parameters); sqlDA.SelectCommand.CommandTimeout = CMDTimeOut; sqlDA.Fill(dataSet, _defaultDataTable); return dataSet; } finally { if (conn != null) conn.Close(); } } /// <summary> /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值) /// </summary> /// <param name="connection">数据库连接</param> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand</returns> private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = new SqlCommand(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (SqlParameter parameter in parameters) { if (parameter != null) { // 检查未分配值的输出参数,将其分配以DBNull.Value. if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) && (parameter.Value == null)) { parameter.Value = DBNull.Value; } command.Parameters.Add(parameter); } } return command; } /// <summary> /// 执行存储过程,返回请求结果 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="rowsAffected">影响的行数</param> /// <returns>请求结果</returns> public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected, string useDB = "") { SqlConnection conn = GetConnection(useDB); try { rowsAffected = 0; if (conn == null) throw new Exception(_linkErrMsg); conn.Open(); int result; SqlCommand command = BuildIntCommand(conn, storedProcName, parameters); rowsAffected = command.ExecuteNonQuery(); result = (int)command.Parameters[_returnParaName].Value; return result; } finally { if (conn != null) conn.Close(); } } /// <summary> /// 创建 SqlCommand 对象实例(用来返回一个整数值) /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand 对象实例</returns> private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new SqlParameter(_returnParaName, SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; } #endregion }
其次,常用的数据库底层操作工具类整理:
/// <summary> /// 数据库操作工具方法类 /// </summary> public class SqlTool { /// <summary> /// string扩展方法(忽略大小写比较字符串) /// </summary> /// <param name="source">源字符串</param> /// <param name="value">目标字符串</param> /// <param name="comparisonType"></param> public static bool Contains(string source, string value, StringComparison comparisonType = StringComparison.OrdinalIgnoreCase) { return source.IndexOf(value, comparisonType) >= 0; } #region 校验方法 /// <summary> /// 验证sql匹配条件是否正确(若以and开头则自动去除) /// </summary> /// <param name="where">sql匹配条件</param> public static string CheckWhere(string where) { if (!String.IsNullOrWhiteSpace(where)) { var str = where.TrimStart();//去除前置空格 if (str.IndexOf("and ", StringComparison.OrdinalIgnoreCase) == 0)//若以and开头则自动去除第一个and { where = str.Substring(4);//若要保留前面一个空格,可以改为3 } //where = filterSql(where);//防SQL注入 if (str.IndexOf("order by", StringComparison.OrdinalIgnoreCase) == 0) { where = " " + where; } else if (!String.IsNullOrWhiteSpace(where)) { where = " where " + where; } } return where; } /// <summary> /// 表名安全校验 /// </summary> /// <param name="tbName">数据表名</param> public static string CheckTbName(string tbName) { //if (tbName.Contains(" ") && !Contains(tbName, " join ")) //非连接语句 //{ // tbName = tbName.Replace(" ", "");//防止SQL注入 //} return tbName; } /// <summary> /// 字段安全校验 /// </summary> /// <param name="fields">字段集合</param> public static string CheckFields(string fields) { //var str = fields.ToLower(); //if (fields.Contains(" ") && !str.Contains(",case ") && str.IndexOf("distinct ") < 0) //{ // str = str.Replace(" as ", "#"); // fields = str.Replace(" ", "").Replace("#", " as "); ;//防止SQL注入 //} return fields; } /// <summary> /// 过滤SQL语句,防止注入 /// </summary> /// <param name="strSql">Sql语句</param> public static string filterSql(string strSql) { var str = strSql.ToLower().Trim(); str = str.Replace("exec", ""); str = str.Replace("delete", ""); str = str.Replace("master", ""); str = str.Replace("truncate", ""); str = str.Replace("declare", ""); str = str.Replace("create", ""); str = str.Replace("xp_", "no"); return str; } #endregion #region 实体赋值方法 /// <summary> /// 类型转换字典 /// </summary> private static Dictionary<Type, Type> TypeDic = new Dictionary<Type, Type>{{typeof(bool?),typeof(bool)},{typeof(int?),typeof(int)}, {typeof(long?),typeof(long)},{typeof(decimal?),typeof(decimal)},{typeof(DateTime?),typeof(DateTime)} }; /// <summary> /// 获取标准类型 /// </summary> public static Type GetStandardType(Type t) { if (TypeDic.ContainsKey(t)) { t = TypeDic[t]; } return t; } /// <summary> /// 根据读取到的数据为实体实例赋值 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="dataReader">读取到的一行数据</param> /// <param name="columns">数据列名集合</param> /// <param name="fields">实体属性集合</param> /// <param name="info">实体实例</param> /// <returns>赋值后的实体实例</returns> public static T GetTValue<T>(IDataReader dataReader, List<string> columns, System.Reflection.PropertyInfo[] fields, T info) where T : class { try { var cols = new List<string>(); cols.AddRange(columns); foreach (var p in fields)//为实体实例赋值 { if (cols.Count < 1) break; var key = p.Name.ToLower(); var idx = cols.IndexOf(key); if (idx >= 0) { cols.RemoveAt(idx); var ovalue = dataReader[p.Name]; if (ovalue == null || ovalue == DBNull.Value) continue; var ptype = GetStandardType(p.PropertyType); p.SetValue(info, Convert.ChangeType(ovalue, ptype), null); } } } catch (Exception ex) { DbLog.LogErr("GetTValue方法异常:" + ex.Message); } return info; } /// <summary> /// 根据读取到的数据为实体实例赋值 /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="row">数据行</param> /// <param name="columns">数据列名集合</param> /// <param name="fields">实体属性集合</param> /// <param name="info">实体实例</param> /// <returns>赋值后的实体实例</returns> public static T GetTValue<T>(DataRow row, List<string> columns, System.Reflection.PropertyInfo[] fields, T info) where T : class { try { var cols = new List<string>(); cols.AddRange(columns); foreach (var p in fields)//为实体实例赋值 { if (cols.Count < 1) break; var key = p.Name.ToLower(); var idx = cols.IndexOf(key); if (idx >= 0) { cols.RemoveAt(idx); var ovalue = row[p.Name]; if (ovalue == null || ovalue == DBNull.Value) continue; var ptype = GetStandardType(p.PropertyType); p.SetValue(info, Convert.ChangeType(ovalue, ptype), null); } } } catch (Exception ex) { DbLog.LogErr("GetTValue1方法异常:" + ex.Message); } return info; } #endregion #region 实例工具方法 /// <summary> /// 对象实例缓存 /// </summary> private static System.Collections.Concurrent.ConcurrentDictionary<string, object> TCache = new System.Collections.Concurrent.ConcurrentDictionary<string, object>(); /// <summary> /// 缓存操作锁 /// </summary> private static object lockCache = new object(); /// <summary> /// 实体克隆方法 /// </summary> private static MethodInfo wiseClone = typeof(object).GetMethod("MemberwiseClone", BindingFlags.NonPublic | BindingFlags.Instance); /// <summary> /// 实体克隆委托方法 /// </summary> public static Func<object, object> doClone = Delegate.CreateDelegate(typeof(Func<object, object>), wiseClone) as Func<object, object>; /// <summary> /// 获取对象实例 /// </summary> /// <param name="type">对象类型</param> public static object GetInstance(Type type) { object obj; if (!TCache.TryGetValue(type.Name, out obj)) { obj = Activator.CreateInstance(type);//构建实例 lock (lockCache) { TCache.TryAdd(type.Name, obj); } } return obj; } /// <summary> /// DataSet转实体集 /// </summary> /// <typeparam name="T">实体类</typeparam> public static List<T> DataSetToList<T>(DataSet ds) where T : class { if (ds != null) { var dt = ds.Tables[0]; return DataTableToList<T>(dt); } return new List<T>(); } /// <summary> /// DataTable转实体集 /// </summary> /// <typeparam name="T">实体类</typeparam> public static List<T> DataTableToList<T>(DataTable dt) where T : class { var list = new List<T>(); if (dt != null && dt.Rows.Count > 0) { var type = typeof(T); var fields = type.GetProperties();//实体属性集合 var columns = new List<string>();//数据列集合 int count = dt.Columns.Count; for (int i = 0; i < count; i++) { columns.Add(dt.Columns[i].ColumnName.ToLower()); } var obj = GetInstance(type); foreach (DataRow row in dt.Rows) { var val = GetTValue<T>(row, columns, fields, doClone(obj) as T); list.Add(val); } } return list; } #endregion }
SQL Server数据库操作帮助类
/// <summary> /// SQL Server数据库操作帮助类 /// </summary> public class MsSqlHelper { /// <summary> /// 参数前缀 /// </summary> private static string PreParameter = "@"; #region 工具方法 // <summary> /// 获取数据插入SQL语句 /// </summary>性能:13字段十万次约0.76s,49字段十万次约1.81s /// <typeparam name="T">实体</typeparam> /// <param name="info">实体实例</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <param name="sqlParams">SQL参数集</param> /// <param name="identify">第一个属性是否为自增主键</param> /// <returns>数据插入SQL语句</returns> private static string GetInsertSQL<T>(T info, string tbName, out List<SqlParameter> sqlParams, bool identify = false) where T : class { var sql = String.Empty; sqlParams = new List<SqlParameter>();//返回值参数 if (String.IsNullOrWhiteSpace(tbName) || info == null) { return sql; } var type = typeof(T); var fields = type.GetProperties();//获取实体成员字段 if (fields.Length < 2) //错误的数据实体 { return sql; } var fmt = "Insert into " + tbName + "({0}) values ({1});"; var cb = new StringBuilder(256);//列集合 var vb = new StringBuilder(512);//值集合 var link = ",";//字段连接符 var start = identify ? 1 : 0; var count = fields.Length; for (int i = start; i < count; i++) { var p = fields[i]; var v = p.GetValue(info, null); if (v == null || v == DBNull.Value) continue;//值为null不处理,使用数据库默认值 cb.Append(link + p.Name); vb.Append(link + PreParameter + p.Name); sqlParams.Add(new SqlParameter(PreParameter + p.Name, v)); } if (cb.Length > 0) //实体属性值不全为null { sql = string.Format(fmt, cb.Remove(0, 1).ToString(), vb.Remove(0, 1).ToString()); } return sql; } /// <summary> /// 获取数据更新SQL语句(要求实体第一个属性必须为主键) /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="info">实体实例</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <param name="sqlParams">SQL参数集</param> /// <returns>数据更新SQL语句</returns> private static string GetUpdateSQL<T>(T info, string tbName, out List<SqlParameter> sqlParams) { var sql = String.Empty; sqlParams = new List<SqlParameter>();//返回值参数 if (String.IsNullOrWhiteSpace(tbName) || info == null) { return sql; } var type = typeof(T); var fields = type.GetProperties();//获取实体成员字段 if (fields.Length < 2) //错误的数据实体 { return sql; } var cv = new StringBuilder(512);//列值对集合 var count = fields.Length; for (int i = 1; i < count; i++) { var p = fields[i]; var v = p.GetValue(info, null); if (v == null || v == DBNull.Value) continue;//值为null不处理,使用数据库默认值 cv.Append("," + p.Name + "=" + PreParameter + p.Name); sqlParams.Add(new SqlParameter(PreParameter + p.Name, v)); } if (cv.Length > 0) //实体属性值不全为null { var sets = cv.Remove(0, 1).ToString(); sqlParams.Add(new SqlParameter(PreParameter + fields[0].Name, fields[0].GetValue(info, null))); sql = string.Format("update {0} set {1} where " + fields[0].Name + "=" + PreParameter + fields[0].Name, tbName, sets); } return sql; } /// <summary> /// 获取数据更新(先删除后添加)SQL语句(要求实体第一个属性必须为主键) /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="info">实体实例</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <param name="sqlParams">SQL参数集</param> /// <param name="identify">第一个属性是否为自增主键</param> /// <returns>数据更新SQL语句</returns> private static string GetUpdateByDelAndAdd<T>(T info, string tbName, out List<SqlParameter> sqlParams, bool identify = false) { var sql = String.Empty; sqlParams = new List<SqlParameter>();//返回值参数 if (String.IsNullOrWhiteSpace(tbName) || info == null) { return sql; } var type = typeof(T); var fields = type.GetProperties();//获取实体成员字段 if (fields.Length < 2) //错误的数据实体 { return sql; } var sb = new StringBuilder(1024); sb.Append("delete from " + tbName + " where " + fields[0].Name + "=" + PreParameter + fields[0].Name); sb.Append("Insert into " + tbName + "({0}) values ({1});"); var cb = new StringBuilder(256);//列集合 var vb = new StringBuilder(512);//值集合 var link = ",";//字段连接符 var start = identify ? 1 : 0; var count = fields.Length; for (int i = start; i < count; i++) { var p = fields[i]; var v = p.GetValue(info, null); if (v == null || v == DBNull.Value) continue;//值为null不处理,使用数据库默认值 cb.Append(link + p.Name); vb.Append(link + PreParameter + p.Name); sqlParams.Add(new SqlParameter(PreParameter + p.Name, v)); } if (cb.Length > 0) //实体属性值不全为null { sql = string.Format(sb.ToString(), cb.Remove(0, 1).ToString(), vb.Remove(0, 1).ToString()); } return sql; } #endregion #region 取数据 /// <summary> /// 获取指定表中指定列的值 /// </summary> /// <param name="ColumnCode">列编码</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的第一行的值</returns> public static string GetColumnValue(string ColumnCode, string tbName, string strWhere, string useDb = "") { var value = String.Empty; tbName = SqlTool.CheckTbName(tbName); ColumnCode = SqlTool.CheckFields(ColumnCode); if (String.IsNullOrEmpty(ColumnCode) || String.IsNullOrEmpty(tbName)) { return value; } var strSql = string.Format("select top 1 {0} from {1}", ColumnCode, tbName); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql += strWhere; } try { var obj = SqlBase.ExecuteScalar(strSql, useDb); value = obj == null ? String.Empty : obj.ToString(); } catch (Exception e) { DbLog.LogErr(string.Format("GetColumnValue方法异常:{0},表_{1} 字段_{2}", e.Message, tbName, ColumnCode)); } return value; } /// <summary> /// 获取满足条件的第一行多列的值 /// </summary> /// <param name="columns">列编码集合</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的首行多列的值</returns> public static List<string> GetValues(List<string> columns, string tbName, string strWhere, string useDb = "") { int step = 0; var value = new List<string>(); tbName = SqlTool.CheckTbName(tbName); if (columns.Count < 1 || String.IsNullOrEmpty(tbName)) { return value; } step = 1; var cols = SqlTool.CheckFields(string.Join(",", columns)); var strSql = string.Format("select top 1 {0} from {1}", cols, tbName); step = 2; strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql += strWhere; } try { step = 3; var ds = SqlBase.Query(strSql, useDb); if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0) { var dr = ds.Tables[0].Rows[0]; var count = columns.Count; for (int i = 0; i < count; i++) { value.Add(dr[i] == null ? "" : dr[i].ToString()); } ds.Dispose(); } } catch (Exception e) { DbLog.LogErr(string.Format("GetValues方法异常:{0} step:{1} 表_{2} 字段_{3}", e.Message, step, tbName, cols)); } return value; } /// <summary> /// 获取指定表中记录总数 /// </summary> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的记录总数</returns> public static int GetRecordCount(string tbName, string strWhere, string useDb = "") { int value = 0; tbName = SqlTool.CheckTbName(tbName); if (String.IsNullOrEmpty(tbName)) { return value; } var strSql = string.Format("select count(1) from {0}", tbName); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql += strWhere; } try { object obj = SqlBase.ExecuteScalar(strSql, useDb); value = obj == null ? 0 : Convert.ToInt32(obj); } catch (Exception ex) { DbLog.LogErr(string.Format("GetRecordCount方法异常:{0},表_{1}", ex.Message, tbName)); } return value; } /// <summary> /// 获取数据集 /// </summary> /// <param name="fields">列集合,形如:col1,col2,...,coln</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="top">获取记录最大数量,0为不限制</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的数据集</returns> public static DataSet GetList(string fields, string tbName, string strWhere, int top = 0, string useDb = "") { DataSet ds = null; tbName = SqlTool.CheckTbName(tbName); fields = SqlTool.CheckFields(fields); if (String.IsNullOrEmpty(tbName)) { return ds; } var strSql = string.Format("select {0} {1} from {2}", top < 1 ? "" : string.Format("top {0}", top), String.IsNullOrEmpty(fields) ? "*" : fields, tbName); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql += strWhere; } try { ds = SqlBase.Query(strSql, useDb); } catch (Exception ex) { DbLog.LogErr(string.Format("GetList方法异常:{0},表_{1}", ex.Message, tbName)); } return ds; } /// <summary> /// 分页获取数据集 /// </summary> /// <param name="fields">列集合,形如:col1,col2,...,coln</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="orderby">排序字段 如:addtime desc</param> /// <param name="pageIndex">当前页号</param> /// <param name="pageSize">每页数据量</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的数据集</returns> public static DataSet GetListByPage(string fields, string tbName, string strWhere, string orderby, int pageSize, int pageIndex, string useDb = "") { DataSet ds = null; tbName = SqlTool.CheckTbName(tbName); fields = SqlTool.CheckFields(fields); if (String.IsNullOrEmpty(fields) || String.IsNullOrEmpty(tbName) || String.IsNullOrEmpty(orderby)) { return ds; } if (pageSize < 1) pageSize = 10;//默认每页10条 if (pageIndex < 1) pageIndex = 1;//默认第一页 int start = (pageIndex - 1) * pageSize + 1; int end = pageIndex * pageSize; var strSql = new StringBuilder(512); strSql.Append("select * from ("); strSql.Append(string.Format("select ROW_NUMBER() OVER (ORDER BY {0}) as row,{1} from {2}", orderby, fields, tbName)); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql.Append(strWhere); } strSql.Append(string.Format(") as T where T.row between {0} and {1}", start, end)); try { ds = SqlBase.Query(strSql.ToString(), useDb); } catch (Exception ex) { DbLog.LogErr(string.Format("GetListByPage方法异常:{0},表_{1}", ex.Message, tbName)); } return ds; } /// <summary> /// 获取对象实体 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> /// <returns>数据实体</returns> public static T GetModel<T>(string tbName, string strWhere, string useDb = "") where T : class { return GetInfo<T>(tbName, strWhere, "*", useDb); } /// <summary> /// 获取对象实体 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="tbName">数据表(可级联)</param> /// <param name="fields">字段集合,形如:col1,col2,...,coln</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> /// <returns>数据实体</returns> public static T GetInfo<T>(string tbName, string strWhere, string fields = "*", string useDb = "") where T : class { T val = null; tbName = SqlTool.CheckTbName(tbName); fields = SqlTool.CheckFields(fields); if (!String.IsNullOrEmpty(tbName)) { var strSql = string.Format("select top 1 {0} from {1}", String.IsNullOrWhiteSpace(fields) ? "*" : fields, tbName); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrWhiteSpace(strWhere)) { strSql += strWhere; } try { var ds = SqlBase.Query(strSql, useDb); if (ds != null && ds.Tables.Count > 0) { var dt = ds.Tables[0]; if (dt != null && dt.Rows.Count > 0) { var columns = new List<string>();//数据列集合 var count = dt.Columns.Count; for (int i = 0; i < count; i++) { columns.Add(dt.Columns[i].ColumnName.ToLower()); } var type = typeof(T); var flist = type.GetProperties();//实体属性集合 var obj = SqlTool.GetInstance(type); val = SqlTool.GetTValue<T>(dt.Rows[0], columns, flist, SqlTool.doClone(obj) as T); } } } catch (Exception ex) { DbLog.LogErr(string.Format("GetInfo方法异常:{0},表_{1}", ex.Message, tbName)); } } return val; } /// <summary> /// 获取实体集合 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="top">要获取的最大记录数量,0为全部</param> /// <param name="cols">字段集合,形如:col1,col2,...,coln</param> /// <param name="useDb">数据库简写</param> /// <returns>满足条件的实体集</returns> public static List<T> GetInfoList<T>(string tbName, string strWhere, int top = 0, string cols = "*", string useDb = "") where T : class { try { var ds = GetList(cols, tbName, strWhere, top, useDb); return SqlTool.DataSetToList<T>(ds); } catch (Exception ex) { DbLog.LogErr(string.Format("GetInfoList方法异常:{0},表_{1}", ex.Message, tbName)); } return new List<T>(); } /// <summary> /// 分页获取实体集 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="orderby">排序字段 如:addtime desc</param> /// <param name="pageIndex">当前页号</param> /// <param name="pageSize">每页数据量</param> /// <param name="useDb">数据库简写</param> /// <returns></returns> public static List<T> GetInfoByPage<T>(string tbName, string strWhere, string orderby, int pageSize, int pageIndex, string useDb = "") where T : class { try { var ds = GetListByPage("*", tbName, strWhere, orderby, pageSize, pageIndex, useDb); return SqlTool.DataSetToList<T>(ds); } catch (Exception ex) { DbLog.LogErr(string.Format("GetInfoByPage方法异常:{0},表_{1}", ex.Message, tbName)); } return new List<T>(); } /// <summary> /// 执行存储过程,返回实体列表 /// </summary> /// <typeparam name="T">实体类</typeparam> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <param name="useDb">数据库简写</param> /// <returns></returns> public static List<T> GetEntityList<T>(string storedProcName, IDataParameter[] parameters, string useDb = "") where T : class { try { var ds = SqlBase.RunProcedure(storedProcName, parameters, useDb); return SqlTool.DataSetToList<T>(ds); } catch (Exception ex) { DbLog.LogErr(string.Format("GetEntityList方法异常:{0},存储过程名称_{1}", ex.Message, storedProcName)); } return new List<T>(); } #endregion #region 添加数据 /// <summary> /// 添加记录到数据库(实体主键为自增整形)(带事务) /// </summary> /// <typeparam name="T">实体(第一个属性必须为自增主键)</typeparam> /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="info">实体实例</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <returns>添加后生成的记录ID</returns> public static long RecordAddIdentity<T>(SqlTransaction trans, T info, string tbName, string useDB = "") where T : class { var list = new List<SqlParameter>(); var sql = GetInsertSQL<T>(info, tbName.Replace(" ", ""), out list, true); if (!String.IsNullOrEmpty(sql)) { try { object obj; if (trans == null) { obj = SqlBase.ExecuteScalar(sql.TrimEnd(‘;‘) + ";Select @@IDENTITY", list.ToArray(), useDB); } else { obj = SqlBase.ExecuteScalar(trans, sql.TrimEnd(‘;‘) + ";Select @@IDENTITY", list.ToArray()); } return obj == null ? 0 : Convert.ToInt64(obj); } catch (Exception ex) { DbLog.LogErr(string.Format("RecordAddIdentity方法异常:{0},表_{1}", ex.Message, tbName)); } } return 0; } /// <summary> /// 添加记录到数据库(带事务) /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="info">实体实例</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <returns>成功:True,失败:False</returns> public static bool RecordAdd<T>(SqlTransaction trans, T info, string tbName, string useDB = "") where T : class { var list = new List<SqlParameter>(); var sql = GetInsertSQL<T>(info, tbName.Replace(" ", ""), out list); if (!String.IsNullOrEmpty(sql)) { try { int rows = 0; if (trans == null) { rows = SqlBase.ExecuteNonQuery(sql, list.ToArray(), useDB); } else { rows = SqlBase.ExecuteNonQuery(trans, sql, list.ToArray()); } return rows > 0; } catch (Exception ex) { DbLog.LogErr(string.Format("RecordAdd方法异常:{0},表_{1}", ex.Message, tbName)); } } return false; } /// <summary> /// 批量插入记录(逐条) /// </summary> /// <typeparam name="T">实体</typeparam> /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <param name="list">数据实体集合</param> /// <param name="identity">实体主键是否为自增整形</param> /// <param name="useDb">数据库简写</param> /// <returns>执行成功的数据数</returns> public static int RecordsInsert<T>(SqlTransaction trans, string tbName, List<T> list, bool identity = false, string useDb = "") where T : class { int val = 0; var count = list.Count; if (identity) { for (int i = 0; i < count; i++) { if (RecordAddIdentity<T>(trans, list[i], tbName, useDb) > 0) { val++; } } } else { for (int i = 0; i < count; i++) { if (RecordAdd<T>(trans, list[i], tbName, useDb)) { val++; } } } return val; } /// <summary> /// 批量插入记录(不支持html数据),不建议使用 /// </summary> /// <param name="tbName">数据表名</param> /// <param name="fields">字段集合 如:col1,col2,...</param> /// <param name="values">值集合(值中不能包含",") 如:‘val1‘,‘val2‘,...</param> /// <param name="useDb">数据库简写</param> /// <returns>受影响行数</returns> public static int RecordInsert(string tbName, string fields, List<string> values, string useDb = "") { if (String.IsNullOrWhiteSpace(tbName) || String.IsNullOrWhiteSpace(fields) || values.Count < 1) { return 0; } var strSql = new StringBuilder(512); strSql.Append(string.Format("insert into {0}({1}) ", tbName.Replace(" ", ""), fields.Replace(" ", ""))); var colLength = fields.Split(‘,‘).Length; var equalLength = false;//字段长度是否与值长度是否相同 var count = values.Count; for (int i = 0; i < count; i++) { if (values[i].Split(‘,‘).Length == colLength) { equalLength = true; if (i == 0) { strSql.Append(" select " + values[i]); } else { strSql.Append(" union all select " + values[i]); } } } if (equalLength) { try { return SqlBase.ExecuteNonQuery(strSql.ToString(), useDb); } catch (Exception ex) { DbLog.LogErr(string.Format("RecordInsert方法异常:{0},表_{1}", ex.Message, tbName)); } } return 0; } #endregion #region 更新数据 /// <summary> /// 更新指定数据库指定表中信息 /// </summary>zlf 2014-12-10 /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="ColAndVal">列+值(形如:col = ‘val‘,col2=‘val2‘)</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <returns>是否更新成功</returns> public static bool SetValue(SqlTransaction trans, string ColAndVal, string tbName, string strWhere, string useDb = "") { bool value = false; if (ColAndVal.Contains(" ") && !SqlTool.Contains(ColAndVal, "case ")) { ColAndVal = ColAndVal.Replace(" ", "");//防止SQL注入 } if (String.IsNullOrEmpty(ColAndVal) || String.IsNullOrEmpty(tbName)) { return false; } var strSql = string.Format("update {0} set {1}", tbName.Replace(" ", ""), ColAndVal); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrEmpty(strWhere)) { strSql += strWhere; } try { int rows = 0; if (trans == null) { rows = SqlBase.ExecuteNonQuery(strSql, useDb); } else { rows = SqlBase.ExecuteNonQuery(trans, strSql); } return rows > 0; } catch (Exception ex) { DbLog.LogErr(string.Format("SetValue方法异常:{0},表_{1}", ex.Message, tbName)); } return value; } /// <summary> /// 更新一条记录(实体第一个属性必须为主键)(带事务) /// </summary> /// <typeparam name="T">实体类 </typeparam> /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="tbName">数据表名(字段>=实体属性)</param> /// <param name="info">数据实体</param> public static bool RecordUpdate<T>(SqlTransaction trans, string tbName, T info, string useDb = "") where T : class { var list = new List<SqlParameter>(); var sql = GetUpdateSQL<T>(info, tbName.Replace(" ", ""), out list); if (!String.IsNullOrEmpty(sql)) { try { int rows = 0; if (trans == null) { rows = SqlBase.ExecuteNonQuery(sql, list.ToArray(), useDb); } else { rows = SqlBase.ExecuteNonQuery(trans, sql, list.ToArray()); } return rows > 0; } catch (Exception ex) { DbLog.LogErr(string.Format("RecordUpdate方法异常:{0},表_{1}", ex.Message, tbName)); } } return false; } #endregion #region 删除数据 /// <summary> /// 物理删除数据行 /// </summary> /// <param name="trans">事务(不使用时,传入null)</param> /// <param name="tbName">数据表名</param> /// <param name="strWhere">匹配条件</param> /// <param name="useDb">数据库简写</param> public static bool DeleteRows(SqlTransaction trans, string tbName, string strWhere, string useDb = "") { var val = false; var strSql = string.Format("delete from {0}", tbName.Replace(" ", "")); strWhere = SqlTool.CheckWhere(strWhere); if (!String.IsNullOrEmpty(strWhere)) { strSql += strWhere; } else { return val; } try { int rows = 0; if (trans == null) { rows = SqlBase.ExecuteNonQuery(strSql, useDb); } else { rows = SqlBase.ExecuteNonQuery(trans, strSql); } return rows > 0; } catch (Exception ex) { DbLog.LogErr(string.Format("DeleteRows方法异常:{0},表_{1}", ex.Message, tbName)); } return val; } #endregion }
最后是操作日志类
/// <summary> /// 数据库操作日志类 /// </summary> public class DbLog { /// <summary> /// 锁 /// </summary> private static object lockObj = new object(); /// <summary> /// 日志路径 /// </summary> private static string logPath = System.AppDomain.CurrentDomain.BaseDirectory + "DbLog\\"; /// <summary> /// 记录运行日志 /// </summary> /// <param name="fileName">日志名称</param> /// <param name="msg">日志信息</param> public static void WriteLog(string msg, string fileName) { //添加排他锁,解决并发写入的问题 System.Threading.Monitor.Enter(lockObj); try { fileName = string.Format("{0}_{1}", DateTime.Now.ToString("yyyyMMdd"), fileName); if (!System.IO.Directory.Exists(logPath)) { System.IO.Directory.CreateDirectory(logPath); } //如果日志文件大小超过了指定最大值,则转存为新的文件 var fi = new System.IO.FileInfo(logPath + "\\" + fileName); if (fi.Exists && fi.Length >= 2 * 1024 * 1024) { fi.MoveTo(logPath + "\\" + DateTime.Now.ToString("yyyyMMddHHmmss") + "_long_" + fileName); } string logContent = msg; logContent = string.Format("{0} {1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff"), msg); using (System.IO.StreamWriter SW = System.IO.File.AppendText(logPath + "\\" + fileName)) { SW.WriteLine(logContent); } } catch { return; } finally { System.Threading.Monitor.Exit(lockObj); } } /// <summary> /// 异步写日志 /// </summary> public static void LogAsync(string msg, string fileName = "Db.log") { var logAsyn = new Action(() => { WriteLog(msg, fileName); }); logAsyn.BeginInvoke(null, null); } /// <summary> /// 记录错误日志 /// </summary> /// <param name="msg">日志信息</param> public static void LogErr(string msg, string fileName = "Err.log") { LogAsync(msg, fileName); } }
原文地址:https://www.cnblogs.com/mqly/p/4269790.html
时间: 2024-10-03 22:42:05