|
|
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks;
namespace NFine.Data.Extensions { /// <summary>
/// 数据库访问辅助类,add by ngye, on 2013-08-14.
/// </summary>
public static class SqlHelper { #region [ 连接串相关 ]
/// <summary>
/// 数据中心DB的连接字符串
/// </summary>
public static string DataCenterConnString = ConfigurationManager.ConnectionStrings["connstr"].ConnectionString; /// <summary>
/// 获取同步服务器的连接
/// </summary>
/// <returns></returns>
public static SqlConnection GetDataCenterConn() { return new SqlConnection(DataCenterConnString); }
/// <summary>
/// 根据连接串获取连接
/// </summary>
/// <returns></returns>
public static SqlConnection GetConnByString(string conn) { return new SqlConnection(conn); }
/// <summary>
/// 测试连接串是否能正确
/// </summary>
/// <param name="connectionString">连接串</param>
/// <returns></returns>
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; }
/// <summary>
/// 测试连接串是否正确
/// </summary>
/// <param name="connectionString">连接串</param>
/// <param name="timeOut">测试的超时秒数</param>
/// <param name="errInfo">错误时输出的错误信息</param>
/// <returns>是否能正常连接</returns>
public static bool TestConnectionString(string connectionString, int timeOut, ref string errInfo) { bool result = true; string[] arr = connectionString.Split(new char[] { ';' }); List<string> list = new List<string>();
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
public static string HttpPost(string url, string body) { try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { throw new Exception(ex.Message); } } #region [ ExecuteNonQuery ]
/// <summary>
/// 根据sql语句和参数,返回受影响行数
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>受影响行数</returns>
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 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 ]
/// <summary>
/// 根据存储过程和参数,返回Scalar结果
/// </summary>
/// <param name="proc">存储过程</param>
/// <param name="spArr">可变参数</param>
/// <returns>object</returns>
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; } }
/// <summary>
/// 根据存储过程和参数,返回Scalar结果,但不加上超时设置 ( 避免循环做 AddTimeout )
/// </summary>
/// <param name="proc">存储过程</param>
/// <param name="spArr">可变参数</param>
/// <returns>object</returns>
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 ]
/// <summary>
/// 根据sql语句和参数,返回Scalar结果
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>object</returns>
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 ]
/// <summary>
/// 根据存储过程及参数,返回DataSet
/// </summary>
/// <returns>DataSet</returns>
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; } }
/// <summary>
/// 根据存储过程及参数,返回DataTable
/// </summary>
/// <returns>DataTable</returns>
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; } }
/// <summary>
/// 根据sql语句和参数,返回DataRow
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataRow</returns>
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 ]
/// <summary>
/// 根据sql语句和参数,返回DataSet
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataSet</returns>
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; } }
/// <summary>
/// 根据sql语句和参数,返回DataTable
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataTable</returns>
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; } }
/// <summary>
/// 根据sql语句和参数,返回DataRow
/// </summary>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataRow</returns>
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]; } #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 ]
/// <summary>
/// 根据sql语句和参数,返回Scalar结果
/// </summary>
/// <param name="connString">连接串</param>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>object</returns>
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 ]
/// <summary>
/// 根据sql语句和参数,返回受影响行数
/// </summary>
/// <param name="connString">连接串</param>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>受影响行数</returns>
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 ]
/// <summary>
/// 根据sql语句和参数,返回DataTable
/// </summary>
/// <param name="connString">连接串</param>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataTable</returns>
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; } }
/// <summary>
/// 根据sql语句和参数,返回DataRow
/// </summary>
/// <param name="connString">连接串</param>
/// <param name="sql">sql语句</param>
/// <param name="spArr">可变参数</param>
/// <returns>DataRow</returns>
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文件路径执行 ]
/// <summary>
/// 执行SQL文件
/// </summary>
/// <param name="connString"></param>
/// <param name="filePath"></param>
/// <returns></returns>
public static bool ExecuteNonQueryWithConnAndSqlFilePath(string connString, string filePath) { string sql = System.IO.File.ReadAllText(filePath); return ExecuteNonQueryWithConnAndGO(connString, sql); } #endregion
#region [ 执行带Go语句 ]
/// <summary>
/// 执行带"GO"的SQL,返回最后一条SQL的受影响行数
/// </summary>
/// <param name="connString">连接串</param>
/// <param name="sql">sql语句</param>
/// <returns>返回最后一条SQL的受影响行数</returns>
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
/// <summary>
/// 设置Sql参数
/// </summary>
/// <param name="parameterName"></param>
/// <param name="value"></param>
/// <param name="dbType"></param>
/// <returns></returns>
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; }
/// <summary>
/// 根据sql及带 in 的id参数列表, 得到DataTable
/// </summary>
/// <param name="sql">带in ( {0} )的sql</param>
/// <param name="ids">以逗号分隔的id字符串</param>
/// <param name="idDbType">id的Sql数据类型</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTableWithIn(string sql, string ids, SqlDbType idDbType) { SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType); return GetDataTableBySql(sql, spArr); }
/// <summary>
/// 根据sql及带 in 的id参数列表, 得到受影响行数
/// </summary>
/// <param name="sql">带in ( {0} )的sql</param>
/// <param name="ids">以逗号分隔的id字符串</param>
/// <param name="idDbType">id的Sql数据类型</param>
/// <returns>DataTable</returns>
public static int ExecuteNonQueryWithIn(string sql, string ids, SqlDbType idDbType) { SqlParameter[] spArr = GetWithInSqlParameters(ref sql, ids, idDbType); return ExecuteNonQuery(sql, spArr); }
#region [ 带 in 不确定参数的执行方法 ]
/// <summary>
/// 获取带 in 的sql参数列表
/// </summary>
/// <param name="sql">带in ( {0} )的sql</param>
/// <param name="ids">以逗号分隔的id字符串</param>
/// <param name="idDbType">id的Sql数据类型</param>
/// <returns>sql参数列表</returns>
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<SqlParameter> spList = new List<SqlParameter>(); 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 的扩展方法类 ]
/// <summary>
/// 扩展方法类
/// </summary>
public static class SqlHelperExtensionMethods { /// <summary>
/// 新建SqlCommand对象时, 自动加上指定的 CommandTimeout. by ngye, on 2013-07-11.
/// </summary>
/// <param name="cmd">SqlCommand对象</param>
/// <returns>SqlCommand对象</returns>
public static SqlCommand AddTimeout(this SqlCommand cmd) { cmd.CommandTimeout = SqlHelper.CommandTimeout; return cmd; }
/// <summary>
/// 新建SqlBulkCopy对象时, 自动加上指定的 BulkCopyTimeout. by ngye, on 2013-08-30.
/// </summary>
/// <param name="cmd">SqlBulkCopy对象</param>
/// <returns>SqlBulkCopy对象</returns>
public static SqlBulkCopy AddTimeout(this SqlBulkCopy bulkCopy) { bulkCopy.BulkCopyTimeout = SqlHelper.CommandTimeout; return bulkCopy; }
/// <summary>
/// 执行cmd得到 DataTable. by ngye, on 2013-08-01
/// </summary>
/// <param name="cmd"></param>
/// <returns></returns>
public static DataTable ExecuteDataTable(this SqlCommand cmd) { DataTable dt = new DataTable(); using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { adapter.Fill(dt); } return dt; }
/// <summary>
/// 为SqlParameter设置参数. by ngye, on 2013-08-15.
/// </summary>
/// <param name="sp"></param>
/// <returns></returns>
public static SqlParameter SetValue(this SqlParameter sp, object value) { sp.Value = value; return sp; }
/// <summary>
/// 为SqlParameter设置SqlDbType. by ngye, on 2013-09-03.
/// </summary>
/// <param name="sp"></param>
/// <returns></returns>
public static SqlParameter SetSqlDbType(this SqlParameter sp, SqlDbType dbType) { sp.SqlDbType = dbType; return sp; }
/// <summary>
/// 对可以为空的值作这样的处理,一旦其为空,就设置为DBNull.value.
/// </summary>
/// <param name="sp"></param>
/// <returns></returns>
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; } public static string HttpPost(string url, string body) { try { Encoding encoding = Encoding.UTF8; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
request.ContentType = "application/json; charset=utf-8";
byte[] buffer = encoding.GetBytes(body); request.ContentLength = buffer.Length; request.GetRequestStream().Write(buffer, 0, buffer.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding)) { return reader.ReadToEnd(); } } catch (WebException ex) { throw new Exception(ex.Message); } }
}
#endregion
}
|