using?System;
using?System.Data;
using?System.Configuration;
using?System.Web;
using?System.Web.Security;
using?System.Collections;
using?System.Data.SqlClient;
?
//////?數(shù)據(jù)庫(kù)的通用訪問(wèn)代碼
///?此類為抽象類,不允許實(shí)例化,在應(yīng)用時(shí)直接調(diào)用即可
///public?abstract?class?SqlHelper
{
????//獲取數(shù)據(jù)庫(kù)連接字符串,其屬于靜態(tài)變量且只讀,項(xiàng)目中所有文檔可以直接使用,但不能修改
????public?static?readonly?string?ConnectionStringLocalTransaction?=?ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
?
????//?哈希表用來(lái)存儲(chǔ)緩存的參數(shù)信息,哈希表可以存儲(chǔ)任意類型的參數(shù)。
????private?static?Hashtable?parmCache?=?Hashtable.Synchronized(new?Hashtable());
?
????//////執(zhí)行一個(gè)不需要返回值的SqlCommand命令,通過(guò)指定專用的連接字符串。
????///?使用參數(shù)數(shù)組形式提供參數(shù)列表?
????/////////?使用示例:
????///??int?result?=?ExecuteNonQuery(connString,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)有效的數(shù)據(jù)庫(kù)連接字符串///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)數(shù)值表示此SqlCommand命令執(zhí)行后影響的行數(shù)public?static?int?ExecuteNonQuery(string?connectionString,?CommandType?cmdType,?string?cmdText,?params?SqlParameter[]?commandParameters)
????{
?
????????SqlCommand?cmd?=?new?SqlCommand();
?
????????using?(SqlConnection?conn?=?new?SqlConnection(connectionString))
????????{
????????????//通過(guò)PrePareCommand方法將參數(shù)逐個(gè)加入到SqlCommand的參數(shù)集合中
????????????PrepareCommand(cmd,?conn,?null,?cmdType,?cmdText,?commandParameters);
????????????int?val?=?cmd.ExecuteNonQuery();
?
????????????//清空SqlCommand中的參數(shù)列表
????????????cmd.Parameters.Clear();
????????????return?val;
????????}
????}
?????
????//////執(zhí)行一條不返回結(jié)果的SqlCommand,通過(guò)一個(gè)已經(jīng)存在的數(shù)據(jù)庫(kù)連接?
????///?使用參數(shù)數(shù)組提供參數(shù)
????/////////?使用示例:??
????///??int?result?=?ExecuteNonQuery(conn,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)現(xiàn)有的數(shù)據(jù)庫(kù)連接///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)數(shù)值表示此SqlCommand命令執(zhí)行后影響的行數(shù)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;
????}
?
????//////?執(zhí)行一條不返回結(jié)果的SqlCommand,通過(guò)一個(gè)已經(jīng)存在的數(shù)據(jù)庫(kù)事物處理?
????///?使用參數(shù)數(shù)組提供參數(shù)
????/////////?使用示例:?
????///??int?result?=?ExecuteNonQuery(trans,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)存在的?sql?事物處理///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)數(shù)值表示此SqlCommand命令執(zhí)行后影響的行數(shù)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;
????}
?
????//////?執(zhí)行一條返回結(jié)果集的SqlCommand命令,通過(guò)專用的連接字符串。
????///?使用參數(shù)數(shù)組提供參數(shù)
????/////////?使用示例:??
????///??SqlDataReader?r?=?ExecuteReader(connString,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)有效的數(shù)據(jù)庫(kù)連接字符串///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)包含結(jié)果的SqlDataReaderpublic?static?SqlDataReader?ExecuteReader(string?connectionString,?CommandType?cmdType,?string?cmdText,?params?SqlParameter[]?commandParameters)
????{
????????SqlCommand?cmd?=?new?SqlCommand();
????????SqlConnection?conn?=?new?SqlConnection(connectionString);
?
????????//?在這里使用try/catch處理是因?yàn)槿绻椒ǔ霈F(xiàn)異常,則SqlDataReader就不存在,
????????//CommandBehavior.CloseConnection的語(yǔ)句就不會(huì)執(zhí)行,觸發(fā)的異常由catch捕獲。
????????//關(guān)閉數(shù)據(jù)庫(kù)連接,并通過(guò)throw再次引發(fā)捕捉到的異常。
????????try
????????{
????????????PrepareCommand(cmd,?conn,?null,?cmdType,?cmdText,?commandParameters);
????????????SqlDataReader?rdr?=?cmd.ExecuteReader(CommandBehavior.CloseConnection);
????????????cmd.Parameters.Clear();
????????????return?rdr;
????????}
????????catch
????????{
????????????conn.Close();
????????????throw;
????????}
????}
?
????//////?執(zhí)行一條返回第一條記錄第一列的SqlCommand命令,通過(guò)專用的連接字符串。?
????///?使用參數(shù)數(shù)組提供參數(shù)
????/////////?使用示例:??
????///??Object?obj?=?ExecuteScalar(connString,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)有效的數(shù)據(jù)庫(kù)連接字符串///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)object類型的數(shù)據(jù),可以通過(guò)?Convert.To{Type}方法轉(zhuǎn)換類型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;
????????}
????}
?
????//////?執(zhí)行一條返回第一條記錄第一列的SqlCommand命令,通過(guò)已經(jīng)存在的數(shù)據(jù)庫(kù)連接。
????///?使用參數(shù)數(shù)組提供參數(shù)
????/////////?使用示例:?
????///??Object?obj?=?ExecuteScalar(connString,?CommandType.StoredProcedure,?"PublishOrders",?new?SqlParameter("@prodid",?24));
????//////一個(gè)已經(jīng)存在的數(shù)據(jù)庫(kù)連接///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///存儲(chǔ)過(guò)程的名字或者?T-SQL?語(yǔ)句///以數(shù)組形式提供SqlCommand命令中用到的參數(shù)列表///返回一個(gè)object類型的數(shù)據(jù),可以通過(guò)?Convert.To{Type}方法轉(zhuǎn)換類型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;
????}
?
????//////?緩存參數(shù)數(shù)組
????//////參數(shù)緩存的鍵值///被緩存的參數(shù)列表public?static?void?CacheParameters(string?cacheKey,?params?SqlParameter[]?commandParameters)
????{
????????parmCache[cacheKey]?=?commandParameters;
????}
?
????//////?獲取被緩存的參數(shù)
????//////用于查找參數(shù)的KEY值///返回緩存的參數(shù)數(shù)組public?static?SqlParameter[]?GetCachedParameters(string?cacheKey)
????{
????????SqlParameter[]?cachedParms?=?(SqlParameter[])parmCache[cacheKey];
?
????????if?(cachedParms?==?null)
????????????return?null;
?
????????//新建一個(gè)參數(shù)的克隆列表
????????SqlParameter[]?clonedParms?=?new?SqlParameter[cachedParms.Length];
?
????????//通過(guò)循環(huán)為克隆參數(shù)列表賦值
????????for?(int?i?=?0,?j?=?cachedParms.Length;?i?<?j;?i++)
????????????//使用clone方法復(fù)制參數(shù)列表中的參數(shù)
????????????clonedParms[i]?=?(SqlParameter)((ICloneable)cachedParms[i]).Clone();
?
????????return?clonedParms;
????}
?
????//////?為執(zhí)行命令準(zhǔn)備參數(shù)
????//////SqlCommand?命令///已經(jīng)存在的數(shù)據(jù)庫(kù)連接///數(shù)據(jù)庫(kù)事物處理///SqlCommand命令類型?(存儲(chǔ)過(guò)程,?T-SQL語(yǔ)句,?等等。)///Command?text,T-SQL語(yǔ)句?例如?Select?*?from?Products///返回帶參數(shù)的命令private?static?void?PrepareCommand(SqlCommand?cmd,?SqlConnection?conn,?SqlTransaction?trans,?CommandType?cmdType,?string?cmdText,?SqlParameter[]?cmdParms)
????{
?
????????//判斷數(shù)據(jù)庫(kù)連接狀態(tài)
????????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);
????????}
????}
}