using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
namespace EasyNet.DBUtility
{
public abstract class DbHelperSQL
{
//資料庫連接字符串(web.config來配置),可以動態改變connectionString支持多資料庫.
private static string _connectionString = AdoHelper.ConnectionString;
protected DbHelperSQL()
{
}
#region 公用方法
///
/// 判斷是否存在某表的某個欄位
///
/// 表名稱
/// 欄位名稱
/// 是否存在
public static bool ColumnExists(string tableName, string columnName)
{
var sql = "select count(1) from syscolumns where [id]=object_id('" + tableName + "') and [name]='" + columnName + "'";
var res = GetSingle(sql);
if (res == null)
{
return false;
}
return Convert.ToInt32(res) > 0;
}
public static int GetMaxID(string FieldName, string TableName)
{
var strsql = "select max(" + FieldName + ")+1 from " + TableName;
var obj = DbHelperSQL.GetSingle(strsql);
return obj == null ? 1 : int.Parse(obj.ToString());
}
public static bool Exists(string strSql)
{
var obj = DbHelperSQL.GetSingle(strSql);
int cmdresult;
cmdresult = (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) ? 0 : int.Parse(obj.ToString());
return cmdresult == 0 ? false : true;
}
///
/// 表是否存在
///
///
///
public static bool TabExists(string TableName)
{
var strsql = "select count(*) from sysobjects where id = object_id(N'[" + TableName + "]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
//string strsql = "SELECT count(*) FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[" + TableName + "]') AND type in (N'U')";
var obj = DbHelperSQL.GetSingle(strsql);
int cmdresult;
cmdresult = (Object.Equals(obj, null)) || (Object.Equals(obj, DBNull.Value)) ? 0 : int.Parse(obj.ToString());
return cmdresult == 0 ? false : true;
}
public static bool Exists(string strSql, params SqlParameter[] cmdParms)
{
var obj = DbHelperSQL.GetSingle(strSql, cmdParms);
int cmdresult;
cmdresult = (Object.Equals(obj, null)) || (Object.Equals(obj, DBNull.Value)) ? 0 : int.Parse(obj.ToString());
return cmdresult == 0 ? false : true;
}
#endregion 公用方法
#region 執行簡單SQL語句
///
/// 執行SQL語句,返回影響的資料數
///
/// SQL語句
/// 影響的資料數
public static int ExecuteSql(string SQLString)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
var rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SqlException e)
{
connection.Close();
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
public static int ExecuteSqlByTime(string SQLString, int Times)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
var rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SqlException e)
{
connection.Close();
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
///
/// 執行多条SQL語句,實現資料庫事務。
///
/// 多条SQL語句
public static int ExecuteSqlTran(List SQLStringList)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
connection.Open();
var cmd = new SqlCommand
{
Connection = connection
};
var tx = connection.BeginTransaction();
cmd.Transaction = tx;
try
{
var count = 0;
foreach (var strsql in SQLStringList)
{
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
count += cmd.ExecuteNonQuery();
}
}
tx.Commit();
return count;
}
catch (Exception)
{
tx.Rollback();
return 0;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 執行带一個存储過程参數的的SQL語句。
///
/// SQL語句
/// 参數内容,比如一個欄位是格式複雜的文章,有特殊符號,可以通過這個方式添加
/// 影響的資料數
public static int ExecuteSql(string SQLString, string content)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var cmd = new SqlCommand(SQLString, connection);
var myParameter = new SqlParameter("@content", SqlDbType.NText)
{
Value = content
};
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
var rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 執行带一個存储過程参數的的SQL語句。
///
/// SQL語句
/// 参數内容,比如一個欄位是格式複雜的文章,有特殊符號,可以通過這個方式添加
/// 影響的資料數
public static object ExecuteSqlGet(string SQLString, string content)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var cmd = new SqlCommand(SQLString, connection);
var myParameter = new SqlParameter("@content", SqlDbType.NText)
{
Value = content
};
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
var obj = cmd.ExecuteScalar();
return (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) ? null : obj;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 向資料庫里插入圖像格式的欄位(和上面情況類似的另一種實例)
///
/// SQL語句
/// 圖像字節,資料庫的欄位類型为image的情況
/// 影響的資料數
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var cmd = new SqlCommand(strSQL, connection);
var myParameter = new SqlParameter("@fs", SqlDbType.Image)
{
Value = fs
};
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
var rows = cmd.ExecuteNonQuery();
return rows;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
/// 執行一条计算查詢結果語句,返回查詢結果{Object}。
///
/// 计算查詢結果語句
/// 查詢結果{Object}
public static object GetSingle(string SQLString)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
var obj = cmd.ExecuteScalar();
return (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) ? null : obj;
}
catch (SqlException e)
{
connection.Close();
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
public static object GetSingle(string SQLString, int Times)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
var obj = cmd.ExecuteScalar();
return (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) ? null : obj;
}
catch (SqlException e)
{
connection.Close();
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
///
/// 執行查詢語句,返回SqlDataReader ( 注意:調用該方法後,一定要對SqlDataReader進行Close )
///
/// 查詢語句
/// SqlDataReader
public static SqlDataReader ExecuteReader(string strSQL)
{
var connection = new SqlConnection(_connectionString);
using (var cmd = new SqlCommand(strSQL, connection))
{
try
{
connection.Open();
var myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
}
}
///
/// 執行查詢語句,返回DataSet
///
/// 查詢語句
/// DataSet
public static DataSet Query(string SQLString)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var ds = new DataSet();
try
{
connection.Open();
using (var command = new SqlDataAdapter(SQLString, connection))
{
command.Fill(ds, nameof(ds));
}
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
public static DataSet Query(string SQLString, int Times)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var ds = new DataSet();
try
{
connection.Open();
using (var command = new SqlDataAdapter(SQLString, connection))
{
command.SelectCommand.CommandTimeout = Times;
command.Fill(ds, nameof(ds));
}
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
connection.Close();
}
return ds;
}
}
#endregion 執行簡單SQL語句
#region 執行带参數的SQL語句
///
/// 執行SQL語句,返回影響的資料數
///
/// SQL語句
/// todo: describe cmdParms parameter on ExecuteSql
/// 影響的資料數
public static int ExecuteSql(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
var rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
///
/// 執行多条SQL語句,實現資料庫事務。
///
/// SQL語句的哈希表(key为sql語句,value是該語句的SqlParameter[])
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(_connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
var cmd = new SqlCommand();
try
{
//循環
foreach (DictionaryEntry myDE in SQLStringList)
{
var cmdText = myDE.Key.ToString();
var cmdParms = (SqlParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
var val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
/// 執行多条SQL語句,實現資料庫事務。
///
/// todo: describe cmdList parameter on ExecuteSqlTran
public static int ExecuteSqlTran(List cmdList)
{
using (SqlConnection conn = new SqlConnection(_connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
var cmd = new SqlCommand();
try
{
var count = 0;
//循環
foreach (CommandInfo myDE in cmdList)
{
var cmdText = myDE.CommandText;
var cmdParms = (SqlParameter[])myDE.Parameters;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
if (myDE.EffentNextType == EffentNextType.WhenHaveContine || myDE.EffentNextType == EffentNextType.WhenNoHaveContine)
{
if (myDE.CommandText.ToLower().IndexOf("count(") == -1)
{
trans.Rollback();
return 0;
}
var obj = cmd.ExecuteScalar();
var isHave = false;
if (obj == null && obj == DBNull.Value)
{
isHave = false;
}
isHave = Convert.ToInt32(obj) > 0;
if (myDE.EffentNextType == EffentNextType.WhenHaveContine && !isHave)
{
trans.Rollback();
return 0;
}
if (myDE.EffentNextType == EffentNextType.WhenNoHaveContine && isHave)
{
trans.Rollback();
return 0;
}
continue;
}
var val = cmd.ExecuteNonQuery();
count += val;
if (myDE.EffentNextType == EffentNextType.ExcuteEffectRows && val == 0)
{
trans.Rollback();
return 0;
}
cmd.Parameters.Clear();
}
trans.Commit();
return count;
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
/// 執行多条SQL語句,實現資料庫事務。
///
/// SQL語句的哈希表(key为sql語句,value是該語句的SqlParameter[])
public static void ExecuteSqlTranWithIndentity(List SQLStringList)
{
using (SqlConnection conn = new SqlConnection(_connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
var cmd = new SqlCommand();
try
{
var indentity = 0;
//循環
foreach (CommandInfo myDE in SQLStringList)
{
var cmdText = myDE.CommandText;
var cmdParms = (SqlParameter[])myDE.Parameters;
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.InputOutput)
{
q.Value = indentity;
}
}
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
var val = cmd.ExecuteNonQuery();
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.Output)
{
indentity = Convert.ToInt32(q.Value);
}
}
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
/// 執行多条SQL語句,實現資料庫事務。
///
/// SQL語句的哈希表(key为sql語句,value是該語句的SqlParameter[])
public static void ExecuteSqlTranWithIndentity(Hashtable SQLStringList)
{
using (SqlConnection conn = new SqlConnection(_connectionString))
{
conn.Open();
using (SqlTransaction trans = conn.BeginTransaction())
{
var cmd = new SqlCommand();
try
{
var indentity = 0;
//循環
foreach (DictionaryEntry myDE in SQLStringList)
{
var cmdText = myDE.Key.ToString();
var cmdParms = (SqlParameter[])myDE.Value;
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.InputOutput)
{
q.Value = indentity;
}
}
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
var val = cmd.ExecuteNonQuery();
foreach (SqlParameter q in cmdParms)
{
if (q.Direction == ParameterDirection.Output)
{
indentity = Convert.ToInt32(q.Value);
}
}
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
/// 執行一条计算查詢結果語句,返回查詢結果{Object}。
///
/// 计算查詢結果語句
/// todo: describe cmdParms parameter on GetSingle
/// 查詢結果{Object}
public static object GetSingle(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
var obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return (Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)) ? null : obj;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
///
/// 執行查詢語句,返回SqlDataReader ( 注意:調用該方法後,一定要對SqlDataReader進行Close )
///
/// todo: describe SQLString parameter on ExecuteReader
/// todo: describe cmdParms parameter on ExecuteReader
/// SqlDataReader
public static SqlDataReader ExecuteReader(string SQLString, params SqlParameter[] cmdParms)
{
var connection = new SqlConnection(_connectionString);
var cmd = new SqlCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
var myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return myReader;
}
catch (SqlException e)
{
throw new Exception("some reason to rethrow", e);
}
}
///
/// 執行查詢語句,返回DataSet
///
/// 查詢語句
/// todo: describe cmdParms parameter on Query
/// DataSet
public static DataSet Query(string SQLString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
var ds = new DataSet();
try
{
da.Fill(ds, nameof(ds));
cmd.Parameters.Clear();
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
return ds;
}
}
}
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
if (cmdText.StartsWith("OSP_"))
cmd.CommandType = CommandType.StoredProcedure;//cmdType;
else
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parameter in cmdParms)
{
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
}
#endregion 執行带参數的SQL語句
#region 存储過程操作
///
/// 執行存储過程,返回SqlDataReader ( 注意:調用該方法後,一定要對SqlDataReader進行Close )
///
/// 存储過程名
/// 存储過程参數
/// SqlDataReader
public static SqlDataReader RunProcedure(string storedProcName, IDataParameter[] parameters)
{
var connection = new SqlConnection(_connectionString);
SqlDataReader returnReader;
connection.Open();
var command = BuildQueryCommand(connection, storedProcName, parameters);
command.CommandType = CommandType.StoredProcedure;
returnReader = command.ExecuteReader(CommandBehavior.CloseConnection);
return returnReader;
}
///
/// 執行存储過程
///
/// 存储過程名
/// 存储過程参數
/// DataSet結果中的表名
/// DataSet
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var dataSet = new DataSet();
connection.Open();
using (var sqlDA = new SqlDataAdapter
{
SelectCommand = BuildQueryCommand(connection, storedProcName, parameters)
})
{
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
}
public static DataSet RunProcedure(string storedProcName, IDataParameter[] parameters, string tableName, int Times)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
var dataSet = new DataSet();
connection.Open();
using (var sqlDA = new SqlDataAdapter
{
SelectCommand = BuildQueryCommand(connection, storedProcName, parameters)
})
{
sqlDA.SelectCommand.CommandTimeout = Times;
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
}
///
/// 构建 SqlCommand 對象(用來返回一個結果集,而不是一個整數值)
///
/// 資料庫連接
/// 存储過程名
/// 存储過程参數
/// SqlCommand
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
var command = new SqlCommand(storedProcName, connection)
{
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;
}
///
/// 執行存储過程,返回影響的行數
///
/// 存储過程名
/// 存储過程参數
/// 影響的行數
///
public static int RunProcedure(string storedProcName, IDataParameter[] parameters, out int rowsAffected)
{
using (SqlConnection connection = new SqlConnection(_connectionString))
{
int result;
connection.Open();
var command = BuildIntCommand(connection, storedProcName, parameters);
rowsAffected = command.ExecuteNonQuery();
result = (int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
return result;
}
}
///
/// 創建 SqlCommand 對象實例(用來返回一個整數值)
///
/// 存储過程名
/// 存储過程参數
/// todo: describe connection parameter on BuildIntCommand
/// SqlCommand 對象實例
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
var command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue",
SqlDbType.Int, 4, ParameterDirection.ReturnValue,
false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
#endregion 存储過程操作
}
}