SqlHelper 类

本文介绍了一个高性能的数据访问组件SqlHelper类,该类提供了多种执行数据库命令的方法,包括执行非查询命令、读取数据、获取单个标量值等。同时支持参数缓存,简化了参数的管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

None.gifusing System;
None.gif
None.gif
using System.Configuration;
None.gif
None.gif
using System.Data;
None.gif
None.gif
using System.Data.SqlClient;
None.gif
None.gif
using System.Collections;
None.gif
None.gif 
None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif    
/**//// <summary>
InBlock.gif
InBlock.gif    
/// SqlHelper类提供很高的数据访问性能, 
InBlock.gif
InBlock.gif    
/// 使用SqlClient类的通用定义.
InBlock.gif
ExpandedBlockEnd.gif    
/// </summary>

None.gif
None.gif    
public abstract class SqlHelper
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
//定义数据库连接串
InBlock.gif

InBlock.gif        
public static readonly string ConnectionStringLocalTransaction = 
InBlock.gif
InBlock.gifConfigurationManager.ConnectionStrings[
"ExamConnectionString"].ConnectionString;
InBlock.gif
InBlock.gif        
//public static readonly string ConnectionStringInventoryDistributedTransaction = 
InBlock.gif

InBlock.gifConfigurationManager.ConnectionStrings[
"SQLConnString1"].ConnectionString;
InBlock.gif
InBlock.gif        
//public static readonly string ConnectionStringOrderDistributedTransaction = 
InBlock.gif

InBlock.gifConfigurationManager.ConnectionStrings[
"SQLConnString3"].ConnectionString;
InBlock.gif
InBlock.gif        
//public static readonly string ConnectionStringProfile = 
InBlock.gif

InBlock.gifConfigurationManager.ConnectionStrings[
"SQLProfileConnString"].ConnectionString;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif        
// 存贮Cache缓存的Hashtable集合
InBlock.gif

InBlock.gif        
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 使用连接字符串,执行一个SqlCommand命令(没有记录返回)
InBlock.gif
InBlock.gif        
/// 使用提供的参数集.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// 示例:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, 

InBlock.gif
InBlock.gif
"PublishOrders"new SqlParameter("@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="connectionString">一个有效的SqlConnection连接串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandParameters">执行命令的参数集</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>受此命令影响的行数</returns>

InBlock.gif
InBlock.gif        
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string 
InBlock.gif
InBlock.gifcmdText, 
params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
using (SqlConnection conn = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif                
int val = cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif
InBlock.gif                
return val;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在一个存在的连接上执行数据库的命令操作
InBlock.gif
InBlock.gif        
/// 使用提供的参数集.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// e.g.:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  int result = ExecuteNonQuery(connection, CommandType.StoredProcedure, 

InBlock.gif
InBlock.gif
"PublishOrders"new SqlParameter("@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="conn">一个存在的数据库连接对象</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandParameters">执行命令的参数集</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>受此命令影响的行数</returns>

InBlock.gif
InBlock.gif        
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string 
InBlock.gif
InBlock.gifcmdText, 
params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            PrepareCommand(cmd, connection, 
null, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif            
int val = cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif
InBlock.gif            
return val;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在一个事务的连接上执行数据库的命令操作
InBlock.gif
InBlock.gif        
/// 使用提供的参数集.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// e.g.:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new 

InBlock.gif
InBlock.gifSqlParameter(
"@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="trans">一个存在的事务</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType (stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandParameters">执行命令的参数集</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>受此命令影响的行数</returns>

InBlock.gif
InBlock.gif        
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string 
InBlock.gif
InBlock.gifcmdText, 
params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif            PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif            
int val = cmd.ExecuteNonQuery();
InBlock.gif
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif
InBlock.gif            
return val;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在一个连接串上执行一个命令,返回一个SqlDataReader对象
InBlock.gif
InBlock.gif        
/// 使用提供的参数.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// e.g.:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, 

InBlock.gif
InBlock.gif
"PublishOrders"new SqlParameter("@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="connectionString">一个有效的SqlConnection连接串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandParameters">执行命令的参数集</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>一个结果集对象SqlDataReader</returns>

InBlock.gif
InBlock.gif        
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, 
InBlock.gif
InBlock.gif
string cmdText, params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif            SqlConnection conn 
= new SqlConnection(connectionString);
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
// 如果不存在要查询的对象,则发生异常
InBlock.gif
InBlock.gif            
// 连接要关闭
InBlock.gif
InBlock.gif            
// CommandBehavior.CloseConnection在异常时不发生作用
InBlock.gif

InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                PrepareCommand(cmd, conn, 
null, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif                SqlDataReader rdr 
= cmd.ExecuteReader(CommandBehavior.CloseConnection);
InBlock.gif
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif
InBlock.gif                
return rdr;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                conn.Close();
InBlock.gif
InBlock.gif                
throw;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在一个连接串上执行一个命令,返回表中第一行,第一列的值
InBlock.gif
InBlock.gif        
/// 使用提供的参数.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// e.g.:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", 

InBlock.gif
InBlock.gif
new SqlParameter("@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="connectionString">一个有效的SqlConnection连接串</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">执行命令的参数集</param>        /// <returns>返回的对

InBlock.gif
InBlock.gif象,在使用时记得类型转换
</returns>
InBlock.gif
InBlock.gif        
public static object ExecuteScalar(string connectionString, CommandType cmdType, string 
InBlock.gif
InBlock.gifcmdText, 
params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
using (SqlConnection connection = new SqlConnection(connectionString))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                PrepareCommand(cmd, connection, 
null, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif                
object val = cmd.ExecuteScalar();
InBlock.gif
InBlock.gif                cmd.Parameters.Clear();
InBlock.gif
InBlock.gif                
return val;
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在一个连接上执行一个命令,返回表中第一行,第一列的值
InBlock.gif
InBlock.gif        
/// 使用提供的参数.
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <remarks>
InBlock.gif
InBlock.gif        
/// e.g.:  
InBlock.gif
ExpandedSubBlockEnd.gif        
///  Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", 

InBlock.gif
InBlock.gif
new SqlParameter("@prodid"24));
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// </remarks>
InBlock.gif
InBlock.gif        
/// <param name="connectionString">一个有效的SqlConnection连接</param>
InBlock.gif
InBlock.gif        
/// <param name="commandType">命令类型CommandType(stored procedure, text, etc.)</param>
InBlock.gif
InBlock.gif        
/// <param name="commandText">存贮过程名称或是一个T-SQL语句串</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <param name="commandParameters">执行命令的参数集</param>        /// <returns>返回的对

InBlock.gif
InBlock.gif象,在使用时记得类型转换
</returns>
InBlock.gif
InBlock.gif        
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string 
InBlock.gif
InBlock.gifcmdText, 
params SqlParameter[] commandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            SqlCommand cmd 
= new SqlCommand();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            PrepareCommand(cmd, connection, 
null, cmdType, cmdText, commandParameters);
InBlock.gif
InBlock.gif            
object val = cmd.ExecuteScalar();
InBlock.gif
InBlock.gif            cmd.Parameters.Clear();
InBlock.gif
InBlock.gif            
return val;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 在缓存中添加参数数组
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <param name="cacheKey">参数的Key</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <param name="cmdParms">参数数组</param>

InBlock.gif
InBlock.gif        
public static void CacheParameters(string cacheKey, params SqlParameter[] 
InBlock.gif
InBlock.gifcommandParameters)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            parmCache[cacheKey] 
= commandParameters;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 提取缓存的参数数组
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <param name="cacheKey">查找缓存的key</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <returns>返回被缓存的参数数组</returns>

InBlock.gif
InBlock.gif        
public static SqlParameter[] GetCachedParameters(string cacheKey)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            SqlParameter[] cachedParms 
= (SqlParameter[])parmCache[cacheKey];
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (cachedParms == null)
InBlock.gif
InBlock.gif                
return null;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            SqlParameter[] clonedParms 
= new SqlParameter[cachedParms.Length];
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
for (int i = 0, j = cachedParms.Length; i < j; i++)
InBlock.gif
InBlock.gif                clonedParms[i] 
= (SqlParameter)((ICloneable)cachedParms[i]).Clone();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
return clonedParms;
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif 
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif
InBlock.gif        
/// 提供一个SqlCommand对象的设置
InBlock.gif
InBlock.gif        
/// </summary>
InBlock.gif
InBlock.gif        
/// <param name="cmd">SqlCommand对象</param>
InBlock.gif
InBlock.gif        
/// <param name="conn">SqlConnection 对象</param>
InBlock.gif
InBlock.gif        
/// <param name="trans">SqlTransaction 对象</param>
InBlock.gif
InBlock.gif        
/// <param name="cmdType">CommandType 如存贮过程,T-SQL</param>
InBlock.gif
InBlock.gif        
/// <param name="cmdText">存贮过程名或查询串</param>
InBlock.gif
ExpandedSubBlockEnd.gif        
/// <param name="cmdParms">命令中用到的参数集</param>

InBlock.gif
InBlock.gif        
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction 
InBlock.gif
InBlock.giftrans, CommandType cmdType, 
string cmdText, SqlParameter[] cmdParms)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (conn.State != ConnectionState.Open)
InBlock.gif
InBlock.gif                conn.Open();
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            cmd.Connection 
= conn;
InBlock.gif
InBlock.gif            cmd.CommandText 
= cmdText;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (trans != null)
InBlock.gif
InBlock.gif                cmd.Transaction 
= trans;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            cmd.CommandType 
= cmdType;
InBlock.gif
InBlock.gif 
InBlock.gif
InBlock.gif            
if (cmdParms != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
foreach (SqlParameter parm in cmdParms)
InBlock.gif
InBlock.gif                    cmd.Parameters.Add(parm);
InBlock.gif
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif
None.gif

转载于:https://www.cnblogs.com/yiki/archive/2007/05/18/751017.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值