using ICS.Data; using NFine.Code; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace NFine.Data.Extensions { /// /// 数据库访问辅助类,add by ngye, on 2013-08-14. /// public static class SqlHelper { #region [ 连接串相关 ] /// /// 数据中心DB的连接字符串 /// public static string DataCenterConnString = FromMd5(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString); public static void ExecuteDataSet(DataSet data) { try { //DBHelper.ExecuteNonQuery(dsconn, CommandType.Text, sql); using (SqlConnection destinationConnection = new SqlConnection(DataCenterConnString)) { destinationConnection.Open();//打开连接 SqlTransaction bulkTran = destinationConnection.BeginTransaction(); using (SqlBulkCopy bulkCopy = new SqlBulkCopy(destinationConnection, SqlBulkCopyOptions.CheckConstraints, bulkTran)) { //将DataTable插入到后台 bulkCopy.BulkCopyTimeout = 30; foreach (DataTable dt in data.Tables) { bulkCopy.DestinationTableName = dt.TableName;//目标数据表名 try { bulkCopy.WriteToServer(dt); } catch (Exception eBulk) { bulkTran.Rollback(); bulkCopy.Close(); destinationConnection.Close(); throw eBulk; } } bulkTran.Commit(); bulkCopy.Close(); } destinationConnection.Close(); } } catch (Exception ex) { throw ex; } } #region 字符串加解密 /// /// MD5加密 /// /// /// public static string ToMd5(string str) { return Encrypt(str, "&%#@?,:*_"); } /// /// MD5解密 /// /// /// public static string FromMd5(string str) { //return str; return Decrypt(str, "&%#@?,:*_"); } /// /// 加密 /// /// /// /// private static String Encrypt(String strText, String strEncrKey) { Byte[] byKey = { }; Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; try { byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Convert.ToBase64String(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } /// /// 解密 /// /// /// /// private static String Decrypt(String strText, String sDecrKey) { Byte[] byKey = { }; Byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; Byte[] inputByteArray = new byte[strText.Length]; try { byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8)); DESCryptoServiceProvider des = new DESCryptoServiceProvider(); inputByteArray = Convert.FromBase64String(strText); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); System.Text.Encoding encoding = System.Text.Encoding.UTF8; return encoding.GetString(ms.ToArray()); } catch (Exception ex) { return ex.Message; } } #endregion /// /// 获取同步服务器的连接 /// /// public static SqlConnection GetDataCenterConn() { return new SqlConnection(DataCenterConnString); } /// /// 根据连接串获取连接 /// /// public static SqlConnection GetConnByStr(string connStr) { string ConnString = FromMd5( ConfigurationManager.ConnectionStrings[connStr].ConnectionString); return new SqlConnection(ConnString); } //public static SqlConnection SetDateBase(){ // ConfigurationManager.ConnectionStrings["connstr"] = MD5DbTXT; // config.Save(); // } /// /// 根据连接串获取连接 /// /// public static SqlConnection GetConnByString(string conn) { return new SqlConnection(conn); } /// /// 测试连接串是否能正确 /// /// 连接串 /// public static bool TestConnectionString(string connectionString) { bool result = true; try { using (SqlConnection conn = GetConnByString(connectionString)) { try { conn.Open(); } catch (Exception ex) { result = false; } } } catch (Exception) { result = false; } return result; } /// /// 测试连接串是否正确 /// /// 连接串 /// 测试的超时秒数 /// 错误时输出的错误信息 /// 是否能正常连接 public static bool TestConnectionString(string connectionString, int timeOut, ref string errInfo) { bool result = true; string[] arr = connectionString.Split(new char[] { ';' }); List list = new List(); foreach (string s in arr) { if (s.ToLower().IndexOf("timeout") == -1) { list.Add(s); } else { list.Add(String.Format("Connection Timeout={0}", timeOut)); } } SqlConnection con = null; try { con = new SqlConnection(string.Join(";", list.ToArray())); con.Open(); } catch (Exception ex) { result = false; errInfo = ex.Message; } finally { if (con != null) { con.Close(); con.Dispose(); con = null; } } return result; } #endregion #region [ 超时设置 ] public static int CommandTimeout { get { return 7200; } } #endregion /// /// DataSet转Json字符串,主子结构 /// 需要建立主子表关系,第一张表为主表 /// 会排除关联列 /// /// /// 关系名称 /// public static string DataSetToJson(DataSet dataSet, string RelationName, string column) { DataRelation dataRelation = new DataRelation(RelationName, dataSet.Tables[0].Columns[column], dataSet.Tables[1].Columns[column]); dataSet.Relations.Add(dataRelation); StringBuilder jsonString = new StringBuilder(); //foreach (DataTable table in dataSet.Tables) //{ DataTable table = dataSet.Tables[0]; jsonString.Append("["); DataRowCollection drc = table.Rows; for (int i = 0; i < drc.Count; i++) { DataRow dataRow = drc[i]; jsonString.Append("{"); for (int j = 0; j < table.Columns.Count; j++) { string strKey = table.Columns[j].ColumnName; if (dataSet.Relations[RelationName].ParentColumns.Select(a => a.Caption).Contains(strKey)) continue; string strValue = dataRow[j].ToString(); Type type = table.Columns[j].DataType; jsonString.Append("\"" + strKey + "\":"); strValue = StringFormat(strValue, type); if (j < table.Columns.Count - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append(",\"" + RelationName + "\":"); DataRow[] drs = dataRow.GetChildRows(RelationName); jsonString.Append("["); foreach (DataRow dr in drs) { DataTable dt = dr.Table; jsonString.Append("{"); for (int j = 0; j < dt.Columns.Count; j++) { string strKey = dt.Columns[j].ColumnName; if (dataSet.Relations[RelationName].ChildColumns.Select(a => a.Caption).Contains(strKey)) continue; string strValue = dr[j].ToString(); Type type = dt.Columns[j].DataType; jsonString.Append("\"" + strKey + "\":"); strValue = StringFormat(strValue, type); if (j < dt.Columns.Count - 1) { jsonString.Append(strValue + ","); } else { jsonString.Append(strValue); } } jsonString.Append("},"); } if (drs.Length > 0) jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); jsonString.Append("},"); } jsonString.Remove(jsonString.Length - 1, 1); jsonString.Append("]"); //} string res = jsonString.ToString(); return res; } /// /// 过滤特殊字符 /// /// 字符串 /// json字符串 private static string String2Json(String s) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < s.Length; i++) { char c = s.ToCharArray()[i]; switch (c) { case '\"': sb.Append("\\\""); break; case '\\': sb.Append("\\\\"); break; case '/': sb.Append("\\/"); break; case '\b': sb.Append("\\b"); break; case '\f': sb.Append("\\f"); break; case '\n': sb.Append("\\n"); break; case '\r': sb.Append("\\r"); break; case '\t': sb.Append("\\t"); break; default: sb.Append(c); break; } } return sb.ToString(); } /// /// 格式化字符型、日期型、布尔型 /// /// /// /// private static string StringFormat(string str, Type type) { if (type == typeof(string)) { str = String2Json(str); str = "\"" + str + "\""; } else if (type == typeof(DateTime)) { str = "\"" + str + "\""; } else if (type == typeof(bool)) { str = str.ToLower(); } else if (type != typeof(string) && string.IsNullOrEmpty(str)) { str = "\"" + str + "\""; } return str; } #region [ ExecuteNonQuery ] /// /// 根据sql语句和参数,返回受影响行数 /// /// sql语句 /// 可变参数 /// 受影响行数 public static int ExecuteNonQuery(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); return cmd.ExecuteNonQuery(); } } public static DataTable CmdExecuteDataTable(string sql) { try { string connString = SqlHelper.DataCenterConnString; SqlConnection conn = new System.Data.SqlClient.SqlConnection(connString); conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { DataTable dt = SqlCommandHelper.SQlReturnData(sql, cmd); cmd.Transaction.Commit(); return dt; } catch (Exception ex) { cmd.Transaction.Rollback(); throw new Exception(ex.Message); } finally { if (conn.State == ConnectionState.Open) { conn.Close(); } conn.Dispose(); } } catch (Exception ex) { throw new Exception(ex.Message); } } //多语句一起事务中执行(Li编写) public static int CmdExecuteNonQueryLi(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlTransaction sqlTran = conn.BeginTransaction(); SqlCommand cmd = new SqlCommand(); cmd.Transaction = sqlTran; cmd.Connection = conn; try { cmd.CommandText = sql; int RES = cmd.ExecuteNonQuery(); cmd.Transaction.Commit(); return RES; } catch (Exception ex) { cmd.Transaction.Rollback(); throw new Exception(ex.Message); } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); conn.Dispose(); } } return 0; } } public static int CmdExecuteNonQuery(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { int RES= cmd.ExecuteNonQuery(); tx.Commit(); return RES; } catch (System.Data.SqlClient.SqlException E) { tx.Rollback(); throw new Exception(E.Message); } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); conn.Dispose(); } } return 0; } } public static int ExecuteNonQuery(string connStr,string sql, params SqlParameter[] spArr) { using (SqlConnection conn = new SqlConnection(connStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); return cmd.ExecuteNonQuery(); } } #endregion #region [ ExecuteScalar_ForProc ] /// /// 根据存储过程和参数,返回Scalar结果 /// /// 存储过程 /// 可变参数 /// object public static object ExecuteScalar_ForProc(string proc, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout(); cmd.CommandType = CommandType.StoredProcedure; if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); object obj = cmd.ExecuteScalar(); return obj; } } /// /// 根据存储过程和参数,返回Scalar结果,但不加上超时设置 ( 避免循环做 AddTimeout ) /// /// 存储过程 /// 可变参数 /// object public static object ExecuteScalar_ForProc_WithoutAddTimeout(string proc, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(proc, conn); cmd.CommandType = CommandType.StoredProcedure; if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); object obj = cmd.ExecuteScalar(); return obj; } } #endregion #region [ ExecuteScalar ] /// /// 根据sql语句和参数,返回Scalar结果 /// /// sql语句 /// 可变参数 /// object public static object ExecuteScalar(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); object obj = cmd.ExecuteScalar(); return obj; } } #endregion #region [ QueryByProc ] /// /// 根据存储过程及参数,返回DataSet /// /// DataSet public static DataSet GetDataSetByProc(string proc, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout(); cmd.CommandType = CommandType.StoredProcedure; if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } } /// /// 根据存储过程及参数,返回DataTable /// /// DataTable public static DataTable GetDataTableByProc(string proc, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(proc, conn).AddTimeout(); cmd.CommandType = CommandType.StoredProcedure; if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); DataTable dt = cmd.ExecuteDataTable(); return dt; } } /// /// 根据sql语句和参数,返回DataRow /// /// sql语句 /// 可变参数 /// DataRow public static DataRow GetDataRowByProc(string proc, params SqlParameter[] spArr) { DataTable dt = GetDataTableByProc(proc, spArr); if (dt == null || dt.Rows.Count == 0) return null; return dt.Rows[0]; } #endregion #region [ Query ] /// /// 根据sql语句和参数,返回DataSet /// /// sql语句 /// 可变参数 /// DataSet public static DataSet GetDataSetBySql(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } } /// /// 根据sql语句和参数,返回DataTable /// /// sql语句 /// 可变参数 /// DataTable public static DataTable GetDataTableBySql(string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetDataCenterConn()) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); DataTable dt = cmd.ExecuteDataTable(); return dt; } } /// /// 根据sql语句和参数,返回DataRow /// /// sql语句 /// 可变参数 /// DataRow public static DataRow GetDataRowBySql(string sql, params SqlParameter[] spArr) { DataTable dt = GetDataTableBySql(sql, spArr); if (dt == null || dt.Rows.Count == 0) return null; return dt.Rows[0]; } /// /// 根据sql语句和参数,返回DataSet /// /// sql语句 /// 数据库链接名称 /// 可变参数 /// DataSet public static DataSet GetDataSetBySql_OtherConn(string sql, string ConnectStr, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByStr(ConnectStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); SqlDataAdapter adapter = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); adapter.Fill(ds); return ds; } } /// /// 根据sql语句和参数,返回DataTable /// /// sql语句 /// 数据库链接名称 /// 可变参数 /// DataTable public static DataTable GetDataTableBySql_OtherConn(string sql, string ConnectStr, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByStr(ConnectStr)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr != null && spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); DataTable dt = cmd.ExecuteDataTable(); return dt; } } /// /// 根据sql语句和参数,返回DataTable 分页显示 /// /// sql语句 /// 数据库链接名称 /// 可变参数 /// DataTable public static DataTable GetDataTableBySql_OtherConn(string sql, string ConnectStr, ref Pagination jqgridparam, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByStr(ConnectStr)) { string orderField = jqgridparam.sidx; string orderType = jqgridparam.sord; int pageIndex = jqgridparam.page; int pageSize = jqgridparam.rows; int totalRow = 0; string OrderBy = ""; if (pageIndex == 0) { pageIndex = 1; } int num = (pageIndex - 1) * pageSize; int num1 = (pageIndex) * pageSize; if (!string.IsNullOrEmpty(orderField)) OrderBy = orderField + " " + orderType + ""; conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr != null && spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); DataTable dt = cmd.ExecuteDataTable(); DataTable dtNew = dt.Clone(); if (dt != null && dt.Rows.Count > 0) { jqgridparam.records = dt.Rows.Count; DataView dv = dt.DefaultView; dv.Sort = OrderBy; dt = dv.ToTable(); if (dt.Rows.Count >= num) { for (int i = num; i < num1 && i < dt.Rows.Count; i++) { dtNew.ImportRow(dt.Rows[i]); } } } return dtNew; } } /// /// 根据sql语句和参数,返回DataRow /// /// sql语句 /// 数据库链接名称 /// 可变参数 /// DataRow public static DataRow GetDataRowBySql_OtherConn(string sql, string ConnectStr, params SqlParameter[] spArr) { DataTable dt = GetDataTableBySql_OtherConn(sql, ConnectStr, spArr); if (dt == null || dt.Rows.Count == 0) return null; return dt.Rows[0]; } #endregion #region [ GetValue ] public static object GetValue(DataRow dr, string fieldName, object replaceValue) { if (dr.IsNull(fieldName)) return replaceValue; return dr[fieldName]; } public static object GetValue(DataRow dr, int idx, object replaceValue) { if (dr.IsNull(idx)) return replaceValue; return dr[idx]; } #endregion #region [ GetString ] public static string GetString(DataRow dr, string fieldName, string replaceValue) { if (dr.IsNull(fieldName)) return replaceValue; return Convert.ToString(dr[fieldName]); } public static string GetString(DataRow dr, int idx, string replaceValue) { if (dr.IsNull(idx)) return replaceValue; return Convert.ToString(dr[idx]); } #endregion #region [ GetDateTime ] public static DateTime GetDateTime(DataRow dr, string fieldName, DateTime replaceValue) { if (dr.IsNull(fieldName)) return replaceValue; return Convert.ToDateTime(dr[fieldName]); } public static DateTime GetDateTime(DataRow dr, int idx, DateTime replaceValue) { if (dr.IsNull(idx)) return replaceValue; return Convert.ToDateTime(dr[idx]); } #endregion #region [ 非数据中心库操作 ] #region [ ExecuteScalar ] /// /// 根据sql语句和参数,返回Scalar结果 /// /// 连接串 /// sql语句 /// 可变参数 /// object public static object ExecuteScalarWithConn(string connString, string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByString(connString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); object obj = cmd.ExecuteScalar(); return obj; } } #endregion #region [ ExecuteNonQuery ] /// /// 根据sql语句和参数,返回受影响行数 /// /// 连接串 /// sql语句 /// 可变参数 /// 受影响行数 public static int ExecuteNonQueryWithConn(string connString, string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByString(connString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); return cmd.ExecuteNonQuery(); } } #endregion #region [ Query ] /// /// 根据sql语句和参数,返回DataTable /// /// 连接串 /// sql语句 /// 可变参数 /// DataTable public static DataTable GetDataTableBySqlWithConn(string connString, string sql, params SqlParameter[] spArr) { using (SqlConnection conn = SqlHelper.GetConnByString(connString)) { conn.Open(); SqlCommand cmd = new SqlCommand(sql, conn).AddTimeout(); if (spArr.Length > 0) cmd.Parameters.AddRange(spArr.SetDBNull()); DataTable dt = cmd.ExecuteDataTable(); return dt; } } /// /// 根据sql语句和参数,返回DataRow /// /// 连接串 /// sql语句 /// 可变参数 /// DataRow public static DataRow GetDataRowBySqlWithConn(string connString, string sql, params SqlParameter[] spArr) { DataTable dt = GetDataTableBySqlWithConn(connString, sql, spArr); if (dt == null || dt.Rows.Count == 0) return null; return dt.Rows[0]; } #endregion #endregion #region [ 根据SQL文件路径执行 ] /// /// 执行SQL文件 /// /// /// /// public static bool ExecuteNonQueryWithConnAndSqlFilePath(string connString, string filePath) { string sql = System.IO.File.ReadAllText(filePath); return ExecuteNonQueryWithConnAndGO(connString, sql); } #endregion #region [ 执行带Go语句 ] /// /// 执行带"GO"的SQL,返回最后一条SQL的受影响行数 /// /// 连接串 /// sql语句 /// 返回最后一条SQL的受影响行数 public static bool ExecuteNonQueryWithConnAndGO(string connString, string sql) { bool result = true; string[] arr = System.Text.RegularExpressions.Regex.Split(sql, @"\bGO\b", System.Text.RegularExpressions.RegexOptions.IgnoreCase); using (SqlConnection conn = GetConnByString(connString)) { conn.Open(); SqlCommand cmd = new SqlCommand().AddTimeout(); cmd.Connection = conn; SqlTransaction tx = conn.BeginTransaction(); cmd.Transaction = tx; try { for (int n = 0; n < arr.Length; n++) { string strsql = arr[n]; if (strsql.Trim().Length > 1 && strsql.Trim().Replace(";", "") != "") { cmd.CommandText = strsql; cmd.ExecuteNonQuery(); } } tx.Commit(); } catch (System.Data.SqlClient.SqlException E) { tx.Rollback(); result = false; //return -1; throw new Exception(E.Message); } finally { if (conn.State != ConnectionState.Closed) { conn.Close(); conn.Dispose(); } } } return result; } #endregion /// /// 设置Sql参数 /// /// /// /// /// public static SqlParameter SetSqlParameter(string parameterName, object value, SqlDbType dbType) { if (value == null || string.IsNullOrEmpty(value.ToString())) { return new SqlParameter(parameterName, DBNull.Value); } SqlParameter sp = new SqlParameter(parameterName, value); sp.SqlDbType = dbType; return sp; } public static void RemoveEmpty(DataTable dt) { List removelist = new List(); for (int i = 0; i < dt.Rows.Count; i++) { bool IsNull = true; for (int j = 0; j < dt.Columns.Count; j++) { if (!string.IsNullOrEmpty(dt.Rows[i][j].ToString().Trim())) { IsNull = false; } } if (IsNull) { removelist.Add(dt.Rows[i]); } } for (int i = 0; i < removelist.Count; i++) { dt.Rows.Remove(removelist[i]); } } /// /// 大数据插入 /// /// 目标库连接 /// 目标表 /// 来源数据 public static void SqlBulkCopyByDatatable( string TableName, DataTable dtSelect) { using (SqlConnection conn = new SqlConnection(DataCenterConnString)) { using (SqlBulkCopy sqlbulkcopy = new SqlBulkCopy(DataCenterConnString, SqlBulkCopyOptions.UseInternalTransaction)) { try { sqlbulkcopy.DestinationTableName = TableName; sqlbulkcopy.BatchSize = 20000; sqlbulkcopy.BulkCopyTimeout = 0;//不限时间 for (int i = 0; i < dtSelect.Columns.Count; i++) { sqlbulkcopy.ColumnMappings.Add(dtSelect.Columns[i].ColumnName, dtSelect.Columns[i].ColumnName); } sqlbulkcopy.WriteToServer(dtSelect); } catch (System.Exception ex) { throw ex; } } } } /// /// 根据sql及带 in 的id参数列表, 得到DataTable /// /// 带in ( {0} )的sql /// 以逗号分隔的id字符串 /// id的Sql数据类型 /// DataTable public static DataTable GetDataTableWithIn(string sql, string ids, SqlDbType idDbType) { SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType); return GetDataTableBySql(sql, spArr); } /// /// 根据sql及带 in 的id参数列表, 得到受影响行数 /// /// 带in ( {0} )的sql /// 以逗号分隔的id字符串 /// id的Sql数据类型 /// DataTable public static int ExecuteNonQueryWithIn(string sql, string ids, SqlDbType idDbType) { SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType); return ExecuteNonQuery(sql, spArr); } #region [ 带 in 不确定参数的执行方法 ] /// /// 获取带 in 的sql参数列表 /// /// 带in ( {0} )的sql /// 以逗号分隔的id字符串 /// id的Sql数据类型 /// sql参数列表 public static SqlParameter[] GetWithInSqlParameters(ref string sql, string ids, SqlDbType idDbType) { if (string.IsNullOrEmpty(ids)) { return null; } string[] idArr = ids.Split(','); //组建sql在in中的字符串 StringBuilder sbCondition = new StringBuilder(); List spList = new List(); for (int i = 0; i < idArr.Length; i++) { string id = idArr[i]; string spName = string.Format("@id{0}", i); sbCondition.AppendFormat("{0},", spName); spList.Add(SetSqlParameter(spName, id, idDbType)); } //重新构建sql sql = string.Format(sql, sbCondition.ToString().TrimEnd(',')); return spList.ToArray(); } #endregion }//end of class #region [ SqlHelper 的扩展方法类 ] /// /// 扩展方法类 /// public static class SqlHelperExtensionMethods { /// /// 新建SqlCommand对象时, 自动加上指定的 CommandTimeout. by ngye, on 2013-07-11. /// /// SqlCommand对象 /// SqlCommand对象 public static SqlCommand AddTimeout(this SqlCommand cmd) { cmd.CommandTimeout = SqlHelper.CommandTimeout; return cmd; } /// /// 新建SqlBulkCopy对象时, 自动加上指定的 BulkCopyTimeout. by ngye, on 2013-08-30. /// /// SqlBulkCopy对象 /// SqlBulkCopy对象 public static SqlBulkCopy AddTimeout(this SqlBulkCopy bulkCopy) { bulkCopy.BulkCopyTimeout = SqlHelper.CommandTimeout; return bulkCopy; } /// /// 执行cmd得到 DataTable. by ngye, on 2013-08-01 /// /// /// public static DataTable ExecuteDataTable(this SqlCommand cmd) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { adapter.Fill(dt); } return dt; } /// /// 为SqlParameter设置参数. by ngye, on 2013-08-15. /// /// /// public static SqlParameter SetValue(this SqlParameter sp, object value) { sp.Value = value; return sp; } /// /// 为SqlParameter设置SqlDbType. by ngye, on 2013-09-03. /// /// /// public static SqlParameter SetSqlDbType(this SqlParameter sp, SqlDbType dbType) { sp.SqlDbType = dbType; return sp; } /// /// 对可以为空的值作这样的处理,一旦其为空,就设置为DBNull.value. /// /// /// public static SqlParameter[] SetDBNull(this SqlParameter[] spArr) { if (spArr == null || spArr.Length == 0) return spArr; for (int i = 0; i < spArr.Length; i++) { SqlParameter sp = spArr[i]; if (sp.Value == null) sp.Value = DBNull.Value; } return spArr; } } #endregion }