马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
一个很大的类库。应用程序之所以难以跨平台,在于直接调用了特定平台的接口,而一个巨大的类库,就能极大地减少应用程序对平台的依赖。<divclass="code">usingSystem;
usingSystem.Collections;
usingSystem.Collections.Generic;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Data.SqlClient;
namespaceDBUtility
{
publicabstractclassDbHelperSQL
{
publicDbHelperSQL()
{
}
//猎取解密后的字符串
protectedstaticstringconnectionString=newSymmetricMethod().Decrypto(ConfigurationSettings.AppSettings["connectionString"].ToString());
publicstaticintGetMaxID(stringFieldName,stringTableName)
{
stringstrsql="selectisnull(max("+FieldName+"),0)+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;
}
}
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;
}
}
///<summary>
///前往毗连
///</summary>
///<returns></returns>
publicstaticSqlConnectionGetConnection()
{
stringcurrentConnectionString=connectionString;
if(!string.IsNullOrEmpty(System.Web.HttpContext.Current.User.Identity.Name))
{
currentConnectionString=currentConnectionString+";ApplicationName=ForegroundUserID="+System.Web.HttpContext.Current.User.Identity.Name;
}
returnnewSqlConnection(currentConnectionString);
}
///<summary>
///实行SQL语句,前往影响的纪录数
///</summary>
///<paramname="SQLString">SQL语句</param>
///<returns>影响的纪录数</returns>
publicstaticintExecuteSql(stringSQLString)
{
using(SqlConnectionconnection=GetConnection())
{
using(SqlCommandcmd=newSqlCommand(SQLString,connection))
{
try
{
connection.Open();
introws=cmd.ExecuteNonQuery();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
{
thrownewException(E.Message);
}
finally
{
connection.Close();
}
}
}
}
///<summary>
///实行一条盘算查询了局语句,前往查询了局(object)。
///</summary>
///<paramname="SQLString">盘算查询了局语句</param>
///<returns>查询了局(object)</returns>
publicstaticobjectGetSingle(stringSQLString)
{
using(SqlConnectionconnection=GetConnection())
{
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)
{
thrownewException(e.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
}
publicstaticobjectExecuteScalar(stringstrSQL)
{
using(SqlConnectionconn=GetConnection())
{
if(conn.State==ConnectionState.Closed)
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText=strSQL;
objectresult=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnresult;
}
}
publicstaticobjectExecuteScalar(stringstrSQL,paramsSqlParameter[]paramter)
{
using(SqlConnectionconn=GetConnection())
{
if(conn.State==ConnectionState.Closed)
conn.Open();
SqlCommandcmd=newSqlCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText=strSQL;
if(paramter!=null)
{
foreach(SqlParameterparinparamter)
{
cmd.Parameters.Add(par);
}
}
objectresult=cmd.ExecuteScalar();
cmd.Parameters.Clear();
returnresult;
}
}
///<summary>
///实行查询语句,前往DataSet
///</summary>
///<paramname="SQLString">查询语句</param>
///<returns>DataSet</returns>
publicstaticDataSetQuery(stringSQLString)
{
using(SqlConnectionconnection=GetConnection())
{
DataSetds=newDataSet();
try
{
connection.Open();
SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
command.Fill(ds,"ds");
}
catch(System.Data.SqlClient.SqlExceptionex)
{
#ifDEBUG
thrownewException(ex.Message+SQLString);
#endif
thrownewException(ex.Message);
}
finally
{
connection.Close();
}
returnds;
}
}
///<summary>
///猎取数据集
///</summary>
///<paramname="SQLString"></param>
///<paramname="dtname"></param>
///<returns></returns>
publicstaticDataSetQuery(stringSQLString,stringdtname)
{
using(SqlConnectionconnection=GetConnection())
{
DataSetds=newDataSet();
try
{
connection.Open();
SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
command.Fill(ds,dtname);
}
catch(System.Data.SqlClient.SqlExceptionex)
{
thrownewException(ex.Message);
}
finally
{
connection.Close();
}
returnds;
}
}
///<summary>
///
///</summary>
///<paramname="SQLString"></param>
///<paramname="dtname"></param>
///<paramname="ds"></param>
///<returns></returns>
publicstaticDataSetQuery(stringSQLString,stringdtname,refDataSetds)
{
using(SqlConnectionconnection=GetConnection())
{
try
{
connection.Open();
SqlDataAdaptercommand=newSqlDataAdapter(SQLString,connection);
command.Fill(ds,dtname);
}
catch(System.Data.SqlClient.SqlExceptionex)
{
thrownewException(ex.Message);
}
finally
{
connection.Close();
}
returnds;
}
}
///<summary>
///实行SQL语句,前往影响的纪录数
///</summary>
///<paramname="SQLString">SQL语句</param>
///<returns>影响的纪录数</returns>
publicstaticintExecuteSql(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=GetConnection())
{
using(SqlCommandcmd=newSqlCommand())
{
try
{
PrepareCommand(cmd,connection,null,SQLString,cmdParms);
introws=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
returnrows;
}
catch(System.Data.SqlClient.SqlExceptionE)
{
thrownewException(E.Message);
}
finally
{
}
}
}
}
///<summary>
///实行多条SQL语句,完成数据库事件。
///</summary>
///<paramname="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SqlParameter[])</param>
publicstaticvoidExecuteSqlTran(HashtableSQLStringList)
{
using(SqlConnectionconn=GetConnection())
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
objectmyDeValue=null;
SqlCommandcmd=newSqlCommand();
try
{
foreach(DictionaryEntrymyDEinSQLStringList)
{
SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
myDeValue=myDE.Value;
PrepareCommand(cmd,conn,trans,myDE.Key.ToString(),cmdParms);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch(Exceptionex)
{
throwex;
}
trans.Commit();
}
}
}
publicstaticList<int>ExecuteSqlTranRunID(IList<DictionaryEntry>SQLStringList,SqlConnectionconn,SqlTransactiontrans)
{
List<int>list=newList<int>();
if(conn.State==ConnectionState.Closed)conn.Open();
SqlCommandcmd=newSqlCommand();
try
{
foreach(DictionaryEntrymyDEinSQLStringList)
{
SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
PrepareCommand(cmd,conn,trans,myDE.Key.ToString(),cmdParms);
list.Add(int.Parse(cmd.ExecuteScalar().ToString()));
cmd.Parameters.Clear();
}
}
catch(Exceptionex)
{
trans.Rollback();
throwex;
}
finally
{
}
returnlist;
}
///<summary>
///实行事件,且事件中第一条Sql的前往值作为前面一切Sql最初一个参数的值
///</summary>
///<paramname="SqlStringList"></param>
publicstaticvoidExecuteSqlTrans(List<DictionaryEntry>SqlStringList)
{
using(SqlConnectionconn=GetConnection())
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
SqlCommandcmd=newSqlCommand();
intidentity=0;
stringcmdText;
SqlParameter[]parameter;
try
{
for(inti=0;i<SqlStringList.Count;i++)
{
cmdText=SqlStringList[i].Key.ToString();
parameter=(SqlParameter[])SqlStringList[i].Value;
if(i==0)
{
PrepareCommand(cmd,conn,trans,cmdText,parameter);
identity=int.Parse(cmd.ExecuteScalar().ToString());
cmd.Parameters.Clear();
}
else
{
if(parameter[parameter.Length-1].Value==DBNull.Value||decimal.Parse(parameter[parameter.Length-1].Value.ToString())==0)
{
parameter[parameter.Length-1].Value=identity;
}
PrepareCommand(cmd,conn,trans,cmdText,parameter);
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
trans.Commit();
}
catch(Exceptione)
{
trans.Rollback();
throwe;
}
}
}
}
///<summary>
///实行多条SQL语句,完成数据库事件。
///</summary>
///<paramname="SQLStringList">SQL语句的有序表(key为sql语句,value是该语句的SqlParameter[])</param>
publicstaticvoidExecuteSqlTran(IList<DictionaryEntry>SQLStringList)
{
using(SqlConnectionconn=GetConnection())
{
conn.Open();
using(SqlTransactiontrans=conn.BeginTransaction())
{
objectmyDeValue=null;
SqlCommandcmd=newSqlCommand();
try
{
//轮回
foreach(DictionaryEntrymyDEinSQLStringList)
{
if(myDE.Key==null||string.IsNullOrEmpty(myDE.Key.ToString()))
{
continue;
}
else
{
stringcmdText=myDE.Key.ToString();
SqlParameter[]cmdParms=(SqlParameter[])myDE.Value;
myDeValue=myDE.Value;
PrepareCommand(cmd,conn,trans,cmdText,cmdParms);
intval=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
trans.Commit();
}
catch(SqlExceptionex)
{
trans.Rollback();
throwex;
}
}
}
}
///<summary>
///实行一条盘算查询了局语句,前往查询了局(object)。
///</summary>
///<paramname="SQLString">盘算查询了局语句</param>
///<returns>查询了局(object)</returns>
publicstaticobjectGetSingle(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=GetConnection())
{
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)
{
thrownewException(e.Message);
}
finally
{
}
}
}
}
///<summary>
///实行查询语句,前往DataSet
///</summary>
///<paramname="SQLString">查询语句</param>
///<returns>DataSet</returns>
publicstaticDataSetQuery(stringSQLString,paramsSqlParameter[]cmdParms)
{
using(SqlConnectionconnection=GetConnection())
{
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)
{
#ifDEBUG
thrownewException(ex.Message+SQLString);
#endif
thrownewException(ex.Message);
}
finally
 Java欺骗了我们那么多年,如今的多核时代,我认为它气数已尽! |