using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Collections;
using
System.Data.SqlClient;
/// <summary>
/// 資料庫的通用訪問程式碼
/// 此類為抽象類,不允許例項化,在應用時直接呼叫即可
/// </summary>
public
abstract
class
SqlHelper
{
public
static
readonly
string
ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings[
"pubsConnectionString"
].ConnectionString;
private
static
Hashtable parmCache = Hashtable.Synchronized(
new
Hashtable());
/// <summary>
///執行一個不需要返回值的SqlCommand命令,通過指定專用的連線字串。
/// 使用引數陣列形式提供引數列表
/// </summary>
/// <remarks>
/// 使用示例:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一個有效的資料庫連線字串</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個數值表示此SqlCommand命令執行後影響的行數</returns>
public
static
int
ExecuteNonQuery(
string
connectionString, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
using
(SqlConnection conn =
new
SqlConnection(connectionString))
{
PrepareCommand(cmd, conn,
null
, cmdType, cmdText, commandParameters);
int
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return
val;
}
}
/// <summary>
///執行一條不返回結果的SqlCommand,通過一個已經存在的資料庫連線
/// 使用引數陣列提供引數
/// </summary>
/// <remarks>
/// 使用示例:
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">一個現有的資料庫連線</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個數值表示此SqlCommand命令執行後影響的行數</returns>
public
static
int
ExecuteNonQuery(SqlConnection connection, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
PrepareCommand(cmd, connection,
null
, cmdType, cmdText, commandParameters);
int
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return
val;
}
/// <summary>
/// 執行一條不返回結果的SqlCommand,通過一個已經存在的資料庫事物處理
/// 使用引數陣列提供引數
/// </summary>
/// <remarks>
/// 使用示例:
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">一個存在的 sql 事物處理</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個數值表示此SqlCommand命令執行後影響的行數</returns>
public
static
int
ExecuteNonQuery(SqlTransaction trans, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int
val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return
val;
}
/// <summary>
/// 執行一條返回結果集的SqlCommand命令,通過專用的連線字串。
/// 使用引數陣列提供引數
/// </summary>
/// <remarks>
/// 使用示例:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一個有效的資料庫連線字串</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個包含結果的SqlDataReader</returns>
public
static
SqlDataReader ExecuteReader(
string
connectionString, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
SqlConnection conn =
new
SqlConnection(connectionString);
try
{
PrepareCommand(cmd, conn,
null
, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return
rdr;
}
catch
{
conn.Close();
throw
;
}
}
/// <summary>
/// 執行一條返回第一條記錄第一列的SqlCommand命令,通過專用的連線字串。
/// 使用引數陣列提供引數
/// </summary>
/// <remarks>
/// 使用示例:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">一個有效的資料庫連線字串</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個object型別的資料,可以通過 Convert.To{Type}方法轉換型別</returns>
public
static
object
ExecuteScalar(
string
connectionString, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
using
(SqlConnection connection =
new
SqlConnection(connectionString))
{
PrepareCommand(cmd, connection,
null
, cmdType, cmdText, commandParameters);
object
val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return
val;
}
}
/// <summary>
/// 執行一條返回第一條記錄第一列的SqlCommand命令,通過已經存在的資料庫連線。
/// 使用引數陣列提供引數
/// </summary>
/// <remarks>
/// 使用示例:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">一個已經存在的資料庫連線</param>
/// <param name="commandType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="commandText">儲存過程的名字或者 T-SQL 語句</param>
/// <param name="commandParameters">以陣列形式提供SqlCommand命令中用到的引數列表</param>
/// <returns>返回一個object型別的資料,可以通過 Convert.To{Type}方法轉換型別</returns>
public
static
object
ExecuteScalar(SqlConnection connection, CommandType cmdType,
string
cmdText,
params
SqlParameter[] commandParameters)
{
SqlCommand cmd =
new
SqlCommand();
PrepareCommand(cmd, connection,
null
, cmdType, cmdText, commandParameters);
object
val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return
val;
}
/// <summary>
/// 快取引數陣列
/// </summary>
/// <param name="cacheKey">引數快取的鍵值</param>
/// <param name="cmdParms">被快取的引數列表</param>
public
static
void
CacheParameters(
string
cacheKey,
params
SqlParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// 獲取被快取的引數
/// </summary>
/// <param name="cacheKey">用於查詢引數的KEY值</param>
/// <returns>返回快取的引數陣列</returns>
public
static
SqlParameter[] GetCachedParameters(
string
cacheKey)
{
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if
(cachedParms ==
null
)
return
null
;
SqlParameter[] clonedParms =
new
SqlParameter[cachedParms.Length];
for
(
int
i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return
clonedParms;
}
/// <summary>
/// 為執行命令準備引數
/// </summary>
/// <param name="cmd">SqlCommand 命令</param>
/// <param name="conn">已經存在的資料庫連線</param>
/// <param name="trans">資料庫事物處理</param>
/// <param name="cmdType">SqlCommand命令型別 (儲存過程, T-SQL語句, 等等。)</param>
/// <param name="cmdText">Command text,T-SQL語句 例如 Select * from Products</param>
/// <param name="cmdParms">返回帶引數的命令</param>
private
static
void
PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType,
string
cmdText, SqlParameter[] cmdParms)
{
if
(conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if
(trans !=
null
)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if
(cmdParms !=
null
)
{
foreach
(SqlParameter parm
in
cmdParms)
cmd.Parameters.Add(parm);
}
}
}