该代码来自:动软.Net代码生成器,安装目录下BaseClass\DbHelperSQL.cs
usingSystem;
usingSystem.Collections;
usingSystem.Collections.Specialized;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Data.Common;
usingSystem.Collections.Generic;
namespaceMaticsoft.DBUtility
{
///<summary>
///数据访问抽象基础类
///Copyright(C)2004-2008ByLiTianPing
///</summary>
publicabstractclassDbHelperSQL
{
//数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
publicstaticstringconnectionString=PubConstant.ConnectionString;
publicDbHelperSQL()
{
}
#region公用方法
///<summary>
///判断是否存在某表的某个字段
///</summary>
///<paramname="tableName">表名称</param>
///<paramname="columnName">列名称</param>
///<returns>是否存在</returns>
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;
}
}
///<summary>
///表是否存在
///</summary>
///<paramname="TableName"></param>
///<returns></returns>
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语句
///<summary>
///执行SQL语句,返回影响的记录数
///</summary>
///<paramname="SQLString">SQL语句</param>
///<returns>影响的记录数</returns>
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;
}
}
}
}
///<summary>
///执行Sql和Oracle滴混合事务
///</summary>
///<paramname="list">SQL命令行列表</param>
///<paramname="oracleCmdSqlList">Oracle命令行列表</param>
///<returns>执行结果0-由于SQL造成事务失败-1由于Oracle造成事务失败1-整体事务执行成功</returns>
publicstaticintExecuteSqlTran(List<CommandInfo>list,List<CommandInfo>oracleCmdSqlList)
{
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;
}
}
}
///<summary>
///执行多条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLStringList">多条SQL语句</param>
publicstaticintExecuteSqlTran(List<String>SQLStringList)
{
using(SqlConnectionconn=newSqlConnection(connectionString))
{
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
SqlTransactiontx=conn.BeginTransaction();
cmd.Transaction=tx;
try
{
intcount=0;
for(intn=0;n<SQLStringList.Count;n++)
{
stringstrsql=SQLStringList[n];
if(strsql.Trim().Length>1)
{
cmd.CommandText=strsql;
count+=cmd.ExecuteNonQuery();
}
}
tx.Commit();
returncount;
}
catch
{
tx.Rollback();
return0;
}
}
}
///<summary>
///执行带一个存储过程参数的的SQL语句。
///</summary>
///<paramname="SQLString">SQL语句</param>
///<paramname="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
///<returns>影响的记录数</returns>
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();
}
}
}
///<summary>
///执行带一个存储过程参数的的SQL语句。
///</summary>
///<paramname="SQLString">SQL语句</param>
///<paramname="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
///<returns>影响的记录数</returns>
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();
}
}
}
///<summary>
///向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
///</summary>
///<paramname="strSQL">SQL语句</param>
///<paramname="fs">图像字节,数据库的字段类型为p_w_picpath的情况</param>
///<returns>影响的记录数</returns>
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();
}
}
}
///<summary>
///执行一条计算查询结果语句,返回查询结果(object)。
///</summary>
///<paramname="SQLString">计算查询结果语句</param>
///<returns>查询结果(object)</returns>
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;
}
}
}
}
///<summary>
///执行查询语句,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///</summary>
///<paramname="strSQL">查询语句</param>
///<returns>SqlDataReader</returns>
publicstaticSqlDataReaderExecuteReader(stringstrSQL)
{
SqlConnectionconnection=newSqlConnection(connectionString);
SqlCommandcmd=newSqlCommand(strSQL,connection);
try
{
connection.Open();
SqlDataReadermyReader=cmd.ExecuteReader(CommandBehavior.CloseConnection);
returnmyReader;
}
catch(System.Data.SqlClient.SqlExceptione)
{
throwe;
}
}
///<summary>
///执行查询语句,返回DataSet
///</summary>
///<paramname="SQLString">查询语句</param>
///<returns>DataSet</returns>
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语句
///<summary>
///执行SQL语句,返回影响的记录数
///</summary>
///<paramname="SQLString">SQL语句</param>
///<returns>影响的记录数</returns>
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;
}
}
}
}
///<summary>
///执行多条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
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;
}
}
}
}
///<summary>
///执行多条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
publicstaticintExecuteSqlTran(System.Collections.Generic.List<CommandInfo>cmdList)
{
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;
}
}
}
}
///<summary>
///执行多条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
publicstaticvoidExecuteSqlTranWithIndentity(System.Collections.Generic.List<CommandInfo>SQLStringList)
{
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;
}
}
}
}
///<summary>
///执行多条SQL语句,实现数据库事务。
///</summary>
///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
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;
}
}
}
}
///<summary>
///执行一条计算查询结果语句,返回查询结果(object)。
///</summary>
///<paramname="SQLString">计算查询结果语句</param>
///<returns>查询结果(object)</returns>
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;
}
}
}
}
///<summary>
///执行查询语句,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///</summary>
///<paramname="strSQL">查询语句</param>
///<returns>SqlDataReader</returns>
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();
//}
}
///<summary>
///执行查询语句,返回DataSet
///</summary>
///<paramname="SQLString">查询语句</param>
///<returns>DataSet</returns>
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存储过程操作
///<summary>
///执行存储过程,返回SqlDataReader(注意:调用该方法后,一定要对SqlDataReader进行Close)
///</summary>
///<paramname="storedProcName">存储过程名</param>
///<paramname="parameters">存储过程参数</param>
///<returns>SqlDataReader</returns>
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;
}
///<summary>
///执行存储过程
///</summary>
///<paramname="storedProcName">存储过程名</param>
///<paramname="parameters">存储过程参数</param>
///<paramname="tableName">DataSet结果中的表名</param>
///<returns>DataSet</returns>
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;
}
}
///<summary>
///构建SqlCommand对象(用来返回一个结果集,而不是一个整数值)
///</summary>
///<paramname="connection">数据库连接</param>
///<paramname="storedProcName">存储过程名</param>
///<paramname="parameters">存储过程参数</param>
///<returns>SqlCommand</returns>
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;
}
///<summary>
///执行存储过程,返回影响的行数
///</summary>
///<paramname="storedProcName">存储过程名</param>
///<paramname="parameters">存储过程参数</param>
///<paramname="rowsAffected">影响的行数</param>
///<returns></returns>
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;
}
}
///<summary>
///创建SqlCommand对象实例(用来返回一个整数值)
///</summary>
///<paramname="storedProcName">存储过程名</param>
///<paramname="parameters">存储过程参数</param>
///<returns>SqlCommand对象实例</returns>
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
}
}
转载于:https://blog.51cto.com/7986598/1302390