SQLHelper(中文注释) sqlhelper类
usingSystem.Collections.Generic;
usingSystem.Data;
usingSystem.Data.OleDb;
usingSystem.Data.SqlClient;
namespaceGeneralLibrary.DbAccess
{
///<summary>数据库访问,支持SQLServer、Access数据库。</summary>
publicclassSQLHelper
{
privateIDbCommand_DbCommand;
privateIDbDataAdapter_DbDataAdapter;
privateIDbTransaction_DbTransaction;
///<summary>数据库访问类。</summary>
publicSQLHelper()
{
if(DbConfig.DbConnection==""||DbConfig.DbConnection==null)
{
thrownewException("链接字符串不能为空!");
}
switch(DbConfig.DbType)
{
caseDbType.Access:
this._DbCommand=newOleDbCommand();
this._DbCommand.Connection=newOleDbConnection(DbConfig.DbConnection);
this._DbDataAdapter=newOleDbDataAdapter();
break;
caseDbType.SQLServer:
this._DbCommand=newSqlCommand();
this._DbCommand.Connection=newSqlConnection(DbConfig.DbConnection);
this._DbDataAdapter=newSqlDataAdapter();
break;
}
}
///<summary>数据库访问类。</summary>
publicSQLHelper(stringstrConn,DbTypedbtype)
{
switch(dbtype)
{
caseDbType.Access:
this._DbCommand=newOleDbCommand();
this._DbCommand.Connection=newOleDbConnection(strConn);
this._DbDataAdapter=newOleDbDataAdapter();
break;
caseDbType.SQLServer:
this._DbCommand=newSqlCommand();
this._DbCommand.Connection=newSqlConnection(strConn);
this._DbDataAdapter=newSqlDataAdapter();
break;
}
}
///<summary>打开数据库连接。</summary>
privatevoidOpenConnection()
{
try
{
if(this._DbCommand.Connection.State==ConnectionState.Closed)
{
this._DbCommand.Connection.Open();
}
}
catch(Exceptiondbex)
{
thrownewException(dbex.Message);
}
}
///<summary>关闭数据库连接。</summary>
privatevoidCloseConnection()
{
if(this._DbCommand.Connection.State==ConnectionState.Open)
{
this._DbCommand.Connection.Close();
}
if(this._DbCommand!=null)
{
this._DbCommand.Dispose();
}
}
///<summary>执行SQL语句。</summary>
///<paramname="cmdText">SQL语句</param>
///<returns>返回影响行数</returns>
publicintExecuteSql(stringcmdText)
{
try
{
this._DbCommand.CommandText=cmdText;
this.OpenConnection();
returnthis._DbCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
finally
{
this.CloseConnection();
}
}
///<summary>执行SQL语句。</summary>
///<paramname="cmdText">SQL语句</param>
///<paramname="cmdParameters">@参数</param>
///<returns>返回影响行数</returns>
publicintExecuteSql(stringcmdText,IDataParameter[]cmdParameters)
{
try
{
this._DbCommand.CommandText=cmdText;
foreach(IDataParameterparmincmdParameters)
{
this._DbCommand.Parameters.Add(parm);
}
this.OpenConnection();
returnthis._DbCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
finally
{
this.CloseConnection();
}
}
///<summary>执行SQL语句。</summary>
///<paramname="cmdText">SQL语句</param>
///<paramname="dt">返回查询结果</param>
publicvoidExecuteSql(stringcmdText,outDataTabledt)
{
try
{
this._DbCommand.CommandText=cmdText;
this._DbDataAdapter.SelectCommand=this._DbCommand;
DataSetds=newDataSet();
this._DbDataAdapter.Fill(ds);
dt=ds.Tables[0];
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
}
///<summary>执行SQL语句。</summary>
///<paramname="cmdText">SQL语句</param>
///<paramname="cmdParameters">@参数</param>
///<paramname="dt">返回查询结果</param>
publicvoidExecuteSql(stringcmdText,IDataParameter[]cmdParameters,outDataTabledt)
{
try
{
this._DbCommand.CommandText=cmdText;
foreach(IDataParameterparmincmdParameters)
{
this._DbCommand.Parameters.Add(parm);
}
this._DbDataAdapter.SelectCommand=this._DbCommand;
DataSetds=newDataSet();
this._DbDataAdapter.Fill(ds);
dt=ds.Tables[0];
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
}
///<summary>执行存储过程。</summary>
///<paramname="procName">存储过程名</param>
///<returns>返回影响行数</returns>
publicintExecuteProc(stringprocName)
{
try
{
this._DbCommand.CommandText=procName;
this._DbCommand.CommandType=CommandType.StoredProcedure;
this.OpenConnection();
returnthis._DbCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
finally
{
this.CloseConnection();
}
}
///<summary>执行存储过程。</summary>
///<paramname="procName">存储过程名</param>
///<paramname="cmdParameters">@参数</param>
///<returns>返回影响行数</returns>
publicintExecuteProc(stringprocName,IDataParameter[]cmdParameters)
{
try
{
this._DbCommand.CommandText=procName;
this._DbCommand.CommandType=CommandType.StoredProcedure;
foreach(IDataParameterparmincmdParameters)
{
this._DbCommand.Parameters.Add(parm);
}
this.OpenConnection();
returnthis._DbCommand.ExecuteNonQuery();
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
finally
{
this.CloseConnection();
}
}
///<summary>执行存储过程。</summary>
///<paramname="procName">存储过程名</param>
///<paramname="dt">返回查询结果</param>
publicvoidExecuteProc(stringprocName,outDataTabledt)
{
try
{
this._DbCommand.CommandText=procName;
this._DbCommand.CommandType=CommandType.StoredProcedure;
this._DbDataAdapter.SelectCommand=this._DbCommand;
DataSetds=newDataSet();
this._DbDataAdapter.Fill(ds);
dt=ds.Tables[0];
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
}
///<summary>执行存储过程。</summary>
///<paramname="procName">存储过程名</param>
///<paramname="cmdParameters">@参数</param>
///<paramname="dt">返回查询结果</param>
publicvoidExecuteProc(stringprocName,IDataParameter[]cmdParameters,outDataTabledt)
{
try
{
this._DbCommand.CommandText=procName;
this._DbCommand.CommandType=CommandType.StoredProcedure;
foreach(IDataParameterparmincmdParameters)
{
this._DbCommand.Parameters.Add(parm);
}
this._DbDataAdapter.SelectCommand=this._DbCommand;
DataSetds=newDataSet();
this._DbDataAdapter.Fill(ds);
dt=ds.Tables[0];
}
catch(Exceptionex)
{
this.CloseConnection();
thrownewException(ex.Message);
}
}
///<summary>执行事务。</summary>
///<paramname="cmdTexts">SQL语句</param>
///<returns></returns>
publicboolExecuteTransaction(string[]cmdTexts)
{
try
{
this.OpenConnection();
this._DbTransaction=this._DbCommand.Connection.BeginTransaction();
this._DbCommand.Transaction=this._DbTransaction;
foreach(stringcmdTextincmdTexts)
{
this._DbCommand.CommandText=cmdText;
this._DbCommand.ExecuteNonQuery();
}
this._DbTransaction.Commit();
}
catch
{
this._DbTransaction.Rollback();
this.CloseConnection();
returnfalse;//执行失败
}
returntrue;//执行成功
}
///<summary>执行事务。</summary>
///<paramname="cmdTexts">SQL语句。</param>
///<paramname="lstParameter">@参数</param>
///<paramname="count">次数</param>
///<returns></returns>
publicboolExecuteTransaction(string[]cmdTexts,List<IDataParameter[]>lstParameter,intcount)
{
try
{
this.OpenConnection();
this._DbTransaction=this._DbCommand.Connection.BeginTransaction();
this._DbCommand.Transaction=this._DbTransaction;
for(inti=0;i<count;i++)
{
this._DbCommand.CommandText=cmdTexts[i];
foreach(IDataParameterparminlstParameter[i])
{
this._DbCommand.Parameters.Add(parm);
}
this._DbCommand.ExecuteNonQuery();
}
this._DbTransaction.Commit();
}
catch
{
this._DbTransaction.Rollback();
this.CloseConnection();
returnfalse;//执行失败
}
returntrue;
}
}
}
更多阅读
一个非常合理的经济类期刊分类 经济类期刊投稿
1武汉大学经济与管理学院学术期刊分级方案中文期刊期刊分三类:第一类为奖励期刊;第二类为重要期刊;第三类为核心期刊。一、奖励期刊(一)按一级学科认定的奖励期刊每个一级学科认定 2 种中文奖励期刊,原则上为各学科排位前列的期刊,奖励期刊
转载 是美男啊朴信惠默默无语韩文歌词中文翻译拼音注释 默默无语音译歌词
原文地址:是美男啊朴信惠默默无语韩文歌词中文翻译拼音注释作者:糖糖铉空하지 말걸 그랬어 모른척 해버릴걸han ji man guo ku li sou mu run
2014泰国恐怖片《凌晨三点2/鬼三惊2》720p.BD中文字幕 凌晨三点鬼三惊雪莉
译 名:3点终再勾魂/鬼三惊2/凌晨三点2片 名:3 A.M. 3D: Part 2年 代:2014国 家:泰国类 别:恐怖语 言:泰语字 幕:中文文件格式:BD-RMVB视频尺寸:1280 x 720文件大小:1CD片 长:10
教父1-3合集含原声大碟 DVD-RMVB 中文字幕 CNXP原创组 教父电影原声
【CNXP原创组】【美国经典系列】【教父1-3合集含原声大碟】【DVD-RMVB】【中文字幕】发布者:天字@CNXP ◎译 名 教父◎片 名 The Godfather◎年 代 1972◎国 家 美国◎类 别 犯罪/剧情
中文圣经注释资料大全 查经资料总汇
2010 bodani:伯大尼——无花果之家闽ICP备09011843号【分卷及分章查经资料】旧约圣经01 创世记02 出埃及记03 利未记04 民数记05 申命记06 约书亚记07 士师记08 路得记09 撒母耳记上10 撒母耳记下