中文解释的SqlHelpercs类.docx
- 文档编号:30567365
- 上传时间:2023-08-16
- 格式:DOCX
- 页数:14
- 大小:16.17KB
中文解释的SqlHelpercs类.docx
《中文解释的SqlHelpercs类.docx》由会员分享,可在线阅读,更多相关《中文解释的SqlHelpercs类.docx(14页珍藏版)》请在冰豆网上搜索。
中文解释的SqlHelpercs类
中文解释的SqlHelper.cs类
//这是在网上找的中文解释的SqlHelper
usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Collections;
namespacecom.DataAccess
{
///
///SqlHelper类提供很高的数据访问性能,
///使用SqlClient类的通用定义.
///
publicabstractclassSqlHelper
{
//定义数据库连接串
publicstaticreadonlystringConnectionStringLocalTransaction=ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
//publicstaticreadonlystringConnectionStringInventoryDistributedTransaction=ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
//publicstaticreadonlystringConnectionStringOrderDistributedTransaction=ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
//publicstaticreadonlystringConnectionStringProfile=ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
//存贮Cache缓存的Hashtable集合
privatestaticHashtableparmCache=Hashtable.Synchronized(newHashtable());
///
///使用连接字符串,执行一个SqlCommand命令(没有记录返回)
///使用提供的参数集.
///
///
///示例:
///intresult=ExecuteNonQuery(connString,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
///
publicstaticintExecuteNonQuery(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
using(SqlConnectionconn=newSqlConnection(connectionString))
{
PrepareCommand(cmd,conn,null,cmdType,cmdText,commandParameters);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnval;
}
}
///
///在一个存在的连接上执行数据库的命令操作
///使用提供的参数集.
///
///
///e.g.:
///intresult=ExecuteNonQuery(connection,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
///
publicstaticintExecuteNonQuery(SqlConnectionconnection,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnval;
}
///
///在一个事务的连接上执行数据库的命令操作
///使用提供的参数集.
///
///
///e.g.:
///intresult=ExecuteNonQuery(trans,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
///
publicstaticintExecuteNonQuery(SqlTransactiontrans,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,commandParameters);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnval;
}
///
///在一个连接串上执行一个命令,返回一个SqlDataReader对象
///使用提供的参数.
///
///
///e.g.:
///SqlDataReaderr=ExecuteReader(connString,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
///
publicstaticSqlDataReaderExecuteReader(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
SqlConnectionconn=newSqlConnection(connectionString);
//如果不存在要查询的对象,则发生异常
//连接要关闭
//CommandBehavior.CloseConnection在异常时不发生作用
try
{
PrepareCommand(cmd,conn,null,cmdType,cmdText,commandParameters);
SqlDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
returnrdr;
}
catch
{
conn.Close();
throw;
}
}
///
///在一个连接串上执行一个命令,返回表中第一行,第一列的值
///使用提供的参数.
///
///
///e.g.:
///Objectobj=ExecuteScalar(connString,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
publicstaticobjectExecuteScalar(stringconnectionString,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);
objectval=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnval;
}
}
///
///在一个连接上执行一个命令,返回表中第一行,第一列的值
///使用提供的参数.
///
///
///e.g.:
///Objectobj=ExecuteScalar(connString,CommandType.StoredProcedure,"PublishOrders",newSqlParameter("@prodid",24));
///
///
///
///
///
publicstaticobjectExecuteScalar(SqlConnectionconnection,CommandTypecmdType,stringcmdText,paramsSqlParameter[]commandParameters)
{
SqlCommandcmd=newSqlCommand();
PrepareCommand(cmd,connection,null,cmdType,cmdText,commandParameters);
objectval=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnval;
}
///
///在缓存中添加参数数组
///
///
///
publicstaticvoidCacheParameters(stringcacheKey,paramsSqlParameter[]commandParameters)
{
parmCache[cacheKey]=commandParameters;
}
///
///提取缓存的参数数组
///
///
///
publicstaticSqlParameter[]GetCachedParameters(stringcacheKey)
{
SqlParameter[]cachedParms=(SqlParameter[])parmCache[cacheKey];
if(cachedParms==null)
returnnull;
SqlParameter[]clonedParms=newSqlParameter[cachedParms.Length];
for(inti=0,j=cachedParms.Length;i clonedParms[i]=(SqlParameter)((ICloneable)cachedParms[i]).Clone(); returnclonedParms; } /// ///提供一个SqlCommand对象的设置 /// /// /// /// /// /// /// privatestaticvoidPrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,CommandTypecmdType,stringcmdText,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(SqlParameterparmincmdParms) cmd.Parameters.Add(parm); } } } }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 中文 解释 SqlHelpercs
![提示](https://static.bdocx.com/images/bang_tan.gif)