?Title:DbHelperSQL.cs
From目田文摘
Time:Tue,18Nov200814:22:27+0000
Author:
URL:aixq/post/1293/
Content:
把李天平老师的代码拿过来学习
原文地址:cnblogs/aivdesign/articles/1263247.html
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Specialized;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Data.Common;
usingSystem.Collections.Generic;
namespaceMaticsoft.DBUtility
{
///
///数据访问抽象基础类
///Copyright(C)2004-2008ByLiTianPing
///
publicabstractclassDbHelperSQL
{
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
publicstaticstringconnectionString=PubConstant.ConnectionString;
publicDbHelperSQL()
{
}
#region公用方法
///
///判断是否存在某表的某个字段
///
///表名称
///列名称
///是否存在
publicstaticboolColumnExists(stringtableName,stringcolumnName)
{
stringsql="selectcount(1)fromsyscolumnswhere[id]=object_id('"+tableName+"')and[name]='"+columnName+"'";
objectres=GetSingle(sql);
if(res==null)
{
returnfalse;
}
returnConvert.ToInt32(res)>0;
}
publicstaticintGetMaxID(stringFieldName,stringTableName)
{
stringstrsql="selectmax("+FieldName+")+1from"+TableName;
objectobj=DbHelperSQL.GetSingle(strsql);
if(obj==null)
{
return1;
}
else
{
returnint.Parse(obj.ToString());
}
}
publicstaticboolExists(stringstrSql)
{
objectobj=DbHelperSQL.GetSingle(strSql);
intcmdresult;
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
cmdresult=0;
}
else
{
cmdresult=int.Parse(obj.ToString());
}
if(cmdresult==0)
{
returnfalse;
}
else
{
returntrue;
}
}
///
///表是否存在
///
///
///
publicstaticboolTabExists(stringTableName)
{
stringstrsql="selectcount(*)fromsysobjectswhereid=object_id(N'["+TableName+"]')andOBJECTPROPERTY(id,N'IsUserTable')=1";
//stringstrsql="SELECTcount(*)FROMsys.objectsWHEREobject_id=OBJECT_ID(N'[dbo].["+TableName+"]')ANDtypein(N'U')";
objectobj=DbHelperSQL.GetSingle(strsql);
intcmdresult;
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
cmdresult=0;
}
else
{
cmdresult=int.Parse(obj.ToString());
}
if(cmdresult==0)
{
returnfalse;
}
else
{
returntrue;
}
}
publicstaticboolExists(stringstrSql,paramsSqlParameter[]cmdParms)
{
objectobj=DbHelperSQL.GetSingle(strSql,cmdParms);
intcmdresult;
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
cmdresult=0;
}
else
{
cmdresult=int.Parse(obj.ToString());
}
if(cmdresult==0)
{
returnfalse;
}
else
{
returntrue;
}
}
#endregion
#region执行简单SQL语句
///
///执行SQL语句,返回影响的记录数
///
///SQL语句
///影响的记录数
publicstaticintExecuteSql(stringSQLString)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
{
try
{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptione)
{
connection.Close();
throwe;
}
}
}
}
publicstaticintExecuteSqlByTime(stringSQLString,intTimes)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
{
try
{
connection.Open();
cmd.CommandTimeout=Times;
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptione)
{
connection.Close();
throwe;
}
}
}
}
///
///执行Sql和Oracle滴混合事务
///
///SQL命令行列表
///Oracle命令行列表
///执行结果0-由于SQL造成事务失败-1由于Oracle造成事务失败1-整体事务执行成功
publicstaticintExecuteSqlTran(Listlist,ListoracleCmdSqlList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
SqlTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
foreach(CommandInfomyDEinlist)
{
stringcmdText=myDE.CommandText;
SqlParameter[]cmdParms=(SqlParameter[])myDE.Parameters;
PrepareCommand(cmd,conn,tx,cmdText,cmdParms);
if(myDE.EffentNextType==EffentNextType.SolicitationEvent)
{
if(myDE.CommandText.ToLower().IndexOf("count(")==-1)
{
tx.Rollback();
thrownewException("违背要求"+myDE.CommandText+"必须符合selectcount(..的格式");
//return0;
}
objectobj=cmd.ExecuteScalar();
boolisHave=false;
if(obj==null&;&;obj==DBNull.Value)
{
isHave=false;
}
isHave=Convert.ToInt32(obj)>0;
if(isHave)
{
//引发事件
myDE.OnSolicitationEvent();
}
}
if(myDE.EffentNextType==EffentNextType.WhenHaveContine||myDE.EffentNextType==EffentNextType.WhenNoHaveContine)
{
if(myDE.CommandText.ToLower().IndexOf("count(")==-1)
{
tx.Rollback();
thrownewException("SQL:违背要求"+myDE.CommandText+"必须符合selectcount(..的格式");
//return0;
}
objectobj=cmd.ExecuteScalar();
boolisHave=false;
if(obj==null&;&;obj==DBNull.Value)
{
isHave=false;
}
isHave=Convert.ToInt32(obj)>0;
if(myDE.EffentNextType==EffentNextType.WhenHaveContine&;&;!isHave)
{
tx.Rollback();
thrownewException("SQL:违背要求"+myDE.CommandText+"返回值必须大于0");
//return0;
}
if(myDE.EffentNextType==EffentNextType.WhenNoHaveContine&;&;isHave)
{
tx.Rollback();
thrownewException("SQL:违背要求"+myDE.CommandText+"返回值必须等于0");
//return0;
}
continue;
}
intval=cmd.ExecuteNonQuery();
if(myDE.EffentNextType==EffentNextType.ExcuteEffectRows&;&;val==0)
{
tx.Rollback();
thrownewException("SQL:违背要求"+myDE.CommandText+"必须有影响行");
//return0;
}
cmd.Parameters.Clear();
}
stringoraConnectionString=PubConstant.GetConnectionString("ConnectionStringPPC");
boolres=OracleHelper.ExecuteSqlTran(oraConnectionString,oracleCmdSqlList);
if(!res)
{
tx.Rollback();
thrownewException("Oracle执行失败");
//return-1;
}
tx.Commit();
return1;
}
catch(System.Data.SqlClient.SqlExceptione)
{
tx.Rollback();
throwe;
}
catch(Exceptione)
{
tx.Rollback();
throwe;
}
}
}
///
///执行多条SQL语句,实现数据库事务。
///
///多条SQL语句
publicstaticintExecuteSqlTran(ListSQLStringList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
SqlTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
intcount=0;
for(intn=0;n {
stringstrsql=SQLStringList[n];
if(strsql.Trim().Length>1)
{
cmd.CommandText=strsql;
count+=cmd.ExecuteNonQuery();
}
}
tx.Commit();
returncount;
}
catch
{
tx.Rollback();
return0;
}
}
}
///
///执行带一个存储过程参数的的SQL语句。
///
///SQL语句
///参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
///影响的记录数
publicstaticintExecuteSql(stringSQLString,stringcontent)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
SqlCommandcmd=newSqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@content",SqlDbType.NText);
myParameter.Value=content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
///执行带一个存储过程参数的的SQL语句。
///
///SQL语句
///参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加
///影响的记录数
publicstaticobjectExecuteSqlGet(stringSQLString,stringcontent)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
SqlCommandcmd=newSqlCommand(SQLString,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@content",SqlDbType.NText);
myParameter.Value=content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
objectobj=cmd.ExecuteScalar();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
returnnull;
}
else
{
returnobj;
}
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
///向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
///
///SQL语句
///图像字节,数据库的字段类型为image的情况
///影响的记录数
publicstaticintExecuteSqlInsertImg(stringstrSQL,byte[]fs)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
SqlCommandcmd=newSqlCommand(strSQL,connection);
System.Data.SqlClient.SqlParametermyParameter=newSystem.Data.SqlClient.SqlParameter("@fs",SqlDbType.Image);
myParameter.Value=fs;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
///
///执行一条计算查询结果语句,返回查询结果(object)。
///
///计算查询结果语句
///查询结果(object)
publicstaticobjectGetSingle(stringSQLString)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
{
try
{
connection.Open();
objectobj=cmd.ExecuteScalar();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
returnnull;
}
else
{
returnobj;
}
}
catch(System.Data.SqlClient.SqlExceptione)
{
connection.Close();
throwe;
}
}
}
}
publicstaticobjectGetSingle(stringSQLString,intTimes)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
{
try
{
connection.Open();
cmd.CommandTimeout=Times;
objectobj=cmd.ExecuteScalar();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
returnnull;
}
else
{
returnobj;
}
}
catch(System.Data.SqlClient.SqlExceptione)
{
connection.Close();
throwe;
}
}
}
}
///
///执行查询语句,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///
///查询语句
///SqlDataReader
publicstaticSqlDataReaderExecuteReader(stringstrSQL)
{
SqlConnectionconnection=newSqlConnection(connectionString);
SqlCommandcmd=newSqlCommand(strSQL,connection);
try
{
connection.Open();
SqlDataReadermyReader=cmd.ExecuteReader(CommandBehavior.CloseConnection);
returnmyReader;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
}
///
///执行查询语句,返回DataSet
///
///查询语句
///DataSet
publicstaticDataSetQuery(stringSQLString)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
DataSetds=newDataSet();
try
{
connection.Open();
SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
command.Fill(ds,"ds");
}
catch(System.Data.SqlClient.SqlExceptionex)
{
thrownewException(ex.Message);
}
returnds;
}
}
publicstaticDataSetQuery(stringSQLString,intTimes)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
DataSetds=newDataSet();
try
{
connection.Open();
SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
command.SelectCommand.CommandTimeout=Times;
command.Fill(ds,"ds");
}
catch(System.Data.SqlClient.SqlExceptionex)
{
thrownewException(ex.Message);
}
returnds;
}
}
#endregion
#region执行带参数的SQL语句
///
///执行SQL语句,返回影响的记录数
///
///SQL语句
///影响的记录数
publicstaticintExecuteSql(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand())
{
try
{
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
introws=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
}
}
}
///
///执行多条SQL语句,实现数据库事务。
///
///SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
publicstaticvoidExecuteSqlTran(HashtableSQLStringList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
SqlCommandcmd=newSqlCommand();
try
{
//循环
foreach(DictionaryEntrymyDEinSQLStringList)
{
stringcmdText=myDE.Key.ToString();
SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
///执行多条SQL语句,实现数据库事务。
///
///SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
publicstaticintExecuteSqlTran(System.Collections.Generic.ListcmdList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
SqlCommandcmd=newSqlCommand();
try
{intcount=0;
//循环
foreach(CommandInfomyDEincmdList)
{
stringcmdText=myDE.CommandText;
SqlParameter[]cmdParms=(SqlParameter[])myDE.Parameters;
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
if(myDE.EffentNextType==EffentNextType.WhenHaveContine||myDE.EffentNextType==EffentNextType.WhenNoHaveContine)
{
if(myDE.CommandText.ToLower().IndexOf("count(")==-1)
{
trans.Rollback();
return0;
}
objectobj=cmd.ExecuteScalar();
boolisHave=false;
if(obj==null&;&;obj==DBNull.Value)
{
isHave=false;
}
isHave=Convert.ToInt32(obj)>0;
if(myDE.EffentNextType==EffentNextType.WhenHaveContine&;&;!isHave)
{
trans.Rollback();
return0;
}
if(myDE.EffentNextType==EffentNextType.WhenNoHaveContine&;&;isHave)
{
trans.Rollback();
return0;
}
continue;
}
intval=cmd.ExecuteNonQuery();
count+=val;
if(myDE.EffentNextType==EffentNextType.ExcuteEffectRows&;&;val==0)
{
trans.Rollback();
return0;
}
cmd.Parameters.Clear();
}
trans.Commit();
returncount;
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
///执行多条SQL语句,实现数据库事务。
///
///SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
publicstaticvoidExecuteSqlTranWithIndentity(System.Collections.Generic.ListSQLStringList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
SqlCommandcmd=newSqlCommand();
try
{
intindentity=0;
//循环
foreach(CommandInfomyDEinSQLStringList)
{
stringcmdText=myDE.CommandText;
SqlParameter[]cmdParms=(SqlParameter[])myDE.Parameters;
foreach(SqlParameterqincmdParms)
{
if(q.Direction==ParameterDirection.InputOutput)
{
q.Value=indentity;
}
}
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
foreach(SqlParameterqincmdParms)
{
if(q.Direction==ParameterDirection.Output)
{
indentity=Convert.ToInt32(q.Value);
}
}
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
///执行多条SQL语句,实现数据库事务。
///
///SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])
publicstaticvoidExecuteSqlTranWithIndentity(HashtableSQLStringList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
SqlCommandcmd=newSqlCommand();
try
{
intindentity=0;
//循环
foreach(DictionaryEntrymyDEinSQLStringList)
{
stringcmdText=myDE.Key.ToString();
SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
foreach(SqlParameterqincmdParms)
{
if(q.Direction==ParameterDirection.InputOutput)
{
q.Value=indentity;
}
}
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
foreach(SqlParameterqincmdParms)
{
if(q.Direction==ParameterDirection.Output)
{
indentity=Convert.ToInt32(q.Value);
}
}
cmd.Parameters.Clear();
}
trans.Commit();
}
catch
{
trans.Rollback();
throw;
}
}
}
}
///
///执行一条计算查询结果语句,返回查询结果(object)。
///
///计算查询结果语句
///查询结果(object)
publicstaticobjectGetSingle(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
using(SqlCommandcmd=newSqlCommand())
{
try
{
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
objectobj=cmd.ExecuteScalar();
cmd.Parameters.Clear();
if((Object.Equals(obj,null))||(Object.Equals(obj,System.DBNull.Value)))
{
returnnull;
}
else
{
returnobj;
}
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
}
}
}
///
///执行查询语句,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///
///查询语句
///SqlDataReader
publicstaticSqlDataReaderExecuteReader(stringSQLString,paramsSqlParameter[]cmdParms)
{
SqlConnectionconnection=newSqlConnection(connectionString);
SqlCommandcmd=newSqlCommand();
try
{
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
SqlDataReadermyReader=cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
returnmyReader;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
//finally
//{
//cmd.Dispose();
//connection.Close();
//}
}
///
///执行查询语句,返回DataSet
///
///查询语句
///DataSet
publicstaticDataSetQuery(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
SqlCommandcmd=newSqlCommand();
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
using(SqlDataAdapterda=newSqlDataAdapter(cmd))
{
DataSetds=newDataSet();
try
{
da.Fill(ds,"ds");
cmd.Parameters.Clear();
}
catch(System.Data.SqlClient.SqlExceptionex)
{
thrownewException(ex.Message);
}
returnds;
}
}
}
privatestaticvoidPrepareCommand(SqlCommandcmd,SqlConnectionconn,SqlTransactiontrans,stringcmdText,SqlParameter[]cmdParms)
{
if(conn.State!=ConnectionState.Open)
conn.Open();
cmd.Connection=conn;
cmd.CommandText=cmdText;
if(trans!=null)
cmd.Transaction=trans;
cmd.CommandType=CommandType.Text;//cmdType;
if(cmdParms!=null)
{
foreach(SqlParameterparameterincmdParms)
{
if((parameter.Direction==ParameterDirection.InputOutput||parameter.Direction==ParameterDirection.Input)&;&;
(parameter.Value==null))
{
parameter.Value=DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
}
#endregion
#region存储过程操作
///
///执行存储过程,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///
///存储过程名
///存储过程参数
///SqlDataReader
publicstaticSqlDataReaderRunProcedure(stringstoredProcName,IDataParameter[]parameters)
{
SqlConnectionconnection=newSqlConnection(connectionString);
SqlDataReaderreturnReader;
connection.Open();
SqlCommandcommand=BuildQueryCommand(connection,storedProcName,parameters);
command.CommandType=CommandType.StoredProcedure;
returnReader=command.ExecuteReader(CommandBehavior.CloseConnection);
returnreturnReader;
}
///
///执行存储过程
///
///存储过程名
///存储过程参数
///DataSet结果中的表名
///DataSet
publicstaticDataSetRunProcedure(stringstoredProcName,IDataParameter[]parameters,stringtableName)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
DataSetdataSet=newDataSet();
connection.Open();
SqlDataAdaptersqlDA=newSqlDataAdapter();
sqlDA.SelectCommand=BuildQueryCommand(connection,storedProcName,parameters);
sqlDA.Fill(dataSet,tableName);
connection.Close();
returndataSet;
}
}
publicstaticDataSetRunProcedure(stringstoredProcName,IDataParameter[]parameters,stringtableName,intTimes)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
DataSetdataSet=newDataSet();
connection.Open();
SqlDataAdaptersqlDA=newSqlDataAdapter();
sqlDA.SelectCommand=BuildQueryCommand(connection,storedProcName,parameters);
sqlDA.SelectCommand.CommandTimeout=Times;
sqlDA.Fill(dataSet,tableName);
connection.Close();
returndataSet;
}
}
///
///构建SqlCommand对象(用来返回一个结果集,而不是一个整数值)
///
///数据库连接
///存储过程名
///存储过程参数
///SqlCommand
privatestaticSqlCommandBuildQueryCommand(SqlConnectionconnection,stringstoredProcName,IDataParameter[]parameters)
{
SqlCommandcommand=newSqlCommand(storedProcName,connection);
command.CommandType=CommandType.StoredProcedure;
foreach(SqlParameterparameterinparameters)
{
if(parameter!=null)
{
//检查未分配值的输出参数,将其分配以DBNull.Value.
if((parameter.Direction==ParameterDirection.InputOutput||parameter.Direction==ParameterDirection.Input)&;&;
(parameter.Value==null))
{
parameter.Value=DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
returncommand;
}
///
///执行存储过程,返回影响的行数
///
///存储过程名
///存储过程参数
///影响的行数
///
publicstaticintRunProcedure(stringstoredProcName,IDataParameter[]parameters,outintrowsAffected)
{
using(SqlConnectionconnection=newSqlConnection(connectionString))
{
intresult;
connection.Open();
SqlCommandcommand=BuildIntCommand(connection,storedProcName,parameters);
rowsAffected=command.ExecuteNonQuery();
result=(int)command.Parameters["ReturnValue"].Value;
//Connection.Close();
returnresult;
}
}
///
///创建SqlCommand对象实例(用来返回一个整数值)
///
///存储过程名
///存储过程参数
///SqlCommand对象实例
privatestaticSqlCommandBuildIntCommand(SqlConnectionconnection,stringstoredProcName,IDataParameter[]parameters)
{
SqlCommandcommand=BuildQueryCommand(connection,storedProcName,parameters);
command.Parameters.Add(newSqlParameter("ReturnValue",
SqlDbType.Int,4,ParameterDirection.ReturnValue,
false,0,0,string.Empty,DataRowVersion.Default,null));
returncommand;
}
#endregion
}
}
GeneratedbyBo-blog2.1.2beta1