本文共 7993 字,大约阅读时间需要 26 分钟。
using
System.Configuration;
using
System.Web.Security;
using
System.Collections;
using
System.Data.SqlClient;
/// 此类为抽象类,不允许实例化,在应用时直接调用即可
public
abstract
class
SqlHelper
public
static
readonly
string
ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings[
"pubsConnectionString"
].ConnectionString;
private
static
Hashtable parmCache = Hashtable.Synchronized(
new
Hashtable());
///执行一个不需要返回值的SqlCommand命令,通过指定专用的连接字符串。
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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();
///执行一条不返回结果的SqlCommand,通过一个已经存在的数据库连接
/// int result = ExecuteNonQuery(conn, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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();
/// 执行一条不返回结果的SqlCommand,通过一个已经存在的数据库事物处理
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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();
/// 执行一条返回结果集的SqlCommand命令,通过专用的连接字符串。
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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);
PrepareCommand(cmd, conn,
null
, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
/// 执行一条返回第一条记录第一列的SqlCommand命令,通过专用的连接字符串。
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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();
/// 执行一条返回第一条记录第一列的SqlCommand命令,通过已经存在的数据库连接。
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// <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();
/// <param name="cacheKey">参数缓存的键值</param>
/// <param name="cmdParms">被缓存的参数列表</param>
public
static
void
CacheParameters(
string
cacheKey,
params
SqlParameter[] commandParameters)
parmCache[cacheKey] = commandParameters;
/// <param name="cacheKey">用于查找参数的KEY值</param>
/// <returns>返回缓存的参数数组</returns>
public
static
SqlParameter[] GetCachedParameters(
string
cacheKey)
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if
(cachedParms ==
null
)
SqlParameter[] clonedParms =
new
SqlParameter[cachedParms.Length];
for
(
int
i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
/// <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)
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
foreach
(SqlParameter parm
in
cmdParms)
cmd.Parameters.Add(parm);
转载地址:http://fcfuo.baihongyu.com/