共享Framework源代码

俗话说“工欲善其事,必先利其器”,最近在做一个项目,为了省些事,索性把
以前的Framework重写了一次,没想到断断续续写了3个月之久。现在拿来出
来现现眼,请各位高手指正。

DataAccess是一个抽象基类,是其他数据访问类之母。以下正文。
using System;
using System.IO;
using System.Data;
using System.Diagnostics;

namespace TMT.Framework.Data
{
/// <summary>
/// 数据访问的抽象类:访问不同类型数据的抽象类
/// 所有访问数据相关的基类
/// </summary>
public abstract class DataAccess
{
/// <summary>
/// 构造函数
/// 空
/// </summary>
public DataAccess()
{
// Do nothing.
}

/// <summary>
/// 得到DataSet的虚方法。
/// </summary>
/// <returns></returns>
public virtual DataSet GetDataSet()
{
return new DataSet();
}

/// <summary>
/// 得到DataTable的虚方法。
/// 不是必须实装。
/// </summary>
/// <returns></returns>
public virtual DataTable GetDataTable()
{
return new DataTable();
}

/// <summary>
/// 得到DataView的虚方法。
/// </summary>
/// <returns></returns>
public virtual DataView GetDataView()
{
return new DataView();
}
}

/// <summary>
/// 数据访问类的异常类
/// </summary>
public class DataAccessException : ApplicationException
{

/// <summary>
/// 构造函数
/// </summary>
public DataAccessException()
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">信息</param>
public DataAccessException(string message) : base(message)
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="message">消息</param>
/// <param name="ExInner">现在异常原因以外的异常</param>
public DataAccessException(string message,Exception ExInner)
: base(message,ExInner)
{
}
}
}

/* *****************************************************************************
* Copyright (C) 2006KBA All Right Reserved。 *
* ****************************************************************************/


另外,正在做的这个项目打算做成开源的项目。

using System;
using System.Data; //for Dataset etc...
using System.Data.SqlClient; //for DataCommand etc..
using System.Configuration; //for ConfigurationSettings

using TMT.Framework.Common; //for Logger

namespace TMT.Framework.Data
{
/// <summary>
/// 数据访问类:DB访问类
/// 继承自DataAccess
/// 本类的目的
/// 1、DB访问方式的统一
/// 2、减少不良写入的风险
/// 3、提高整体Source的可读性
/// 4、减少DB访问关联的代码量
/// 5、隐藏各种DB访问对象(SqlConnection,SqlCommand,SqlDataAdapter,SqlTransaction,SqlParameter,...)
/// 解除DB访问的混乱。
/// 6、数据容器:DataReader、DataSet等,在Class里没有实现,在子类实现。
/// DataReader没有必要用New声明(通过SqlCommand.ExectueReader()同样可以实现)。
/// </summary>
public class DbAccess : DataAccess
{
//==============================================
// 连接型、非连接型共同使用的成员
//==============================================
/// <summary>
/// 连接字符串
/// </summary>
protected string connectionString;
/// <summary>
/// Connection连接对象
/// </summary>
private SqlConnection sqlConnection;
/// <summary>
/// Command命令对象
/// </summary>
private SqlCommand sqlCommand;
/// <summary>
/// Tranction事务处理对象
/// </summary>
private SqlTransaction sqlTransaction;
/// <summary>
/// 在Transaction开始,连接打开了吗?
/// </summary>
private bool isConnectionOpenAtTransaction;

//==============================================
// 非连接类型使用的成员
//==============================================
/// <summary>
/// DataAdapter
/// </summary>
private SqlDataAdapter sqlDataAdapter;
//----------------------------------------------
/// <summary>
/// 连接重试次数
/// </summary>
private int connectRetryNumber = 1;
/// <summary>
/// 连接重试间隔(毫秒)
/// </summary>
private int connectRetryInterval = 5000;
//----------------------------------------------
/// <summary>
/// 输入输出数据保存实体
/// </summary>
private DataSet dsEntity = null;
/// <summary>
/// 有无输出LOG(静态Static)
/// </summary>
static private bool isLogOutput = false;

/// ============================================
/// 属性
/// ============================================
/// <summary>
/// 命令文本(字符串)Command Text
/// </summary>
public string CommandText
{
get
{
if(sqlCommand != null)
{
return this.sqlCommand.CommandText;
}
else
{
return "";
}
}
}
/// <summary>
/// 连接重试次数(ConnectRetryNumber)
/// </summary>
protected int ConnectRetryNumber
{
get
{
return connectRetryNumber;
}
set
{
connectRetryNumber = value;
}
}
/// <summary>
/// 连接重试间隔(毫秒)
/// </summary>
protected int ConnectRetryInterval
{
get
{
return connectRetryInterval;
}
set
{
connectRetryInterval = value;
}
}
/// <summary>
/// Log输出有无
/// </summary>
static public void SetLogOutput(string isLogOutput)
{
if(isLogOutput.ToLower().Equals("true"))
{
DbAccess.isLogOutput = true;
Logger.Info(null,"DbAccess.SetLogOutput(true)...DB访问Log输出");
}
else
{
DbAccess.isLogOutput = false;
Logger.Info(null,"DvAccess.SetLogoutput(false)...DB访问Log不输出");
}
}

/// <summary>
/// 构造函数
/// </summary>
public DbAccess()
{
//成员变量初始化
this.connectionString = null;
this.sqlConnection = null;
this.sqlCommand = null;
this.sqlTransaction = null;
this.isConnectionOpenAtTransaction = false;
this.sqlDataAdapter = null;
//初始化Class的成员
this.Init();
}

/// <summary>
/// 构造函数(指定超时)
/// </summary>
/// <param name="timeoutSeconds">超时时间(秒)</param>
public DbAccess(string timeoutSeconds)
{
//成员变量初始化
this.connectionString = null;
this.sqlConnection = null;
this.sqlCommand = null;
this.sqlTransaction = null;
this.isConnectionOpenAtTransaction = false;
this.sqlDataAdapter = null;

this.Init(timeoutSeconds);
}

/// <summary>
/// 构造函数(指定超时)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="timeoutSeconds">超时时间(秒)</param>
public DbAccess(string connectionString,string timeoutSeconds)
{
this.connectionString = null;
this.sqlConnection = null;
this.sqlCommand = null;
this.sqlTransaction = null;
this.isConnectionOpenAtTransaction = false;
this.sqlDataAdapter = null;

this.Init(connectionString,timeoutSeconds);
}

/// <summary>
/// 构造函数(指定超时)
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="timeoutSeconds">超时时间(秒)</param>
/// <param name="connectionRetryNumber">连接重试次数</param>
/// <param name="connectionRetryInterval">连接重试间隔</param>
public DbAccess(string connectionString,string timeoutSeconds,int connectionRetryNumber,int connectionRetryInterval)
{
this.connectionString = null;
this.sqlConnection = null;
this.sqlCommand = null;
this.sqlTransaction = null;
this.isConnectionOpenAtTransaction = false;
this.sqlDataAdapter = null;

this.Init(connectionString,timeoutSeconds,connectionRetryNumber,connectionRetryInterval);
}

/// <summary>
/// 初始化函数(不可用)
/// </summary>
protected void Init()
{
//生成Connection
this.sqlConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
//生成SqlCommand
//作为SqlCommand,也可以作为SqlAdapeter的SqlCommand使用
this.sqlCommand = new SqlCommand();
//关联SqlCommand和SqlConnection
this.sqlCommand.Connection = this.sqlConnection;
throw new Exception("This method is not available.");
}

/// <summary>
/// 初始化函数(不可用)
/// </summary>
/// <param name="timeoutSeconds">超时时间(秒)</param>
protected void Init(string timeoutSeconds)
{
//连接字符串
string connectionString = ConfigurationSettings.AppSettings["connectionStringWithTimeout"];
//生成连接
this.sqlConnection = new SqlConnection(connectionString);
//生成SqlCommand
//作为SqlCommand,也可以作为SqlAdapeter的SqlCommand使用
this.sqlCommand = new SqlCommand();
//设置超时时间
this.sqlCommand.CommandTimeout = Int32.Parse(timeoutSeconds);
//关联SqlCommand和SqlConnection
this.sqlCommand.Connection = this.sqlConnection;

throw new Exception("This method is not available.");

}

/// <summary>
/// 初始化函数
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="timeoutSeconds">超时时间(秒)</param>
protected void Init(string connectionString,string timeoutSeconds)
{
//生成连接
this.sqlConnection = new SqlConnection(connectionString);
//生成SqlCommand
//作为SqlCommand,也可以作为SqlAdapeter的SqlCommand使用
this.sqlCommand = new SqlCommand();
//设置超时时间
this.sqlCommand.CommandTimeout = Int32.Parse(timeoutSeconds);
//关联SqlCommand和SqlConnection
this.sqlCommand.Connection = this.sqlConnection;
}

/// <summary>
/// 初始化函数
/// </summary>
/// <param name="connectionString">连接字符串</param>
/// <param name="timeoutSeconds">超时时间(秒)</param>
/// <param name="connectionRetryNumber">连接重试次数</param>
/// <param name="connectionRetryInterval">连接重试间隔</param>
protected void Init(string connectionString,string timeoutSeconds,int connectionRetryNumber,int connectionRetryInterval)
{
//生成连接
this.sqlConnection = new SqlConnection(connectionString);
//生成SqlCommand
//作为SqlCommand,也可以作为SqlAdapeter的SqlCommand使用
this.sqlCommand = new SqlCommand();
//设置超时时间
this.sqlCommand.CommandTimeout = Int32.Parse(timeoutSeconds);
//设置重试次数
this.ConnectRetryNumber = connectionRetryNumber;
//设置重试间隔
this.ConnectRetryInterval = connectionRetryInterval;
//关联SqlCommand和SqlConnection
this.sqlCommand.Connection = this.sqlConnection;
}

/// <summary>
/// 打开连接
/// </summary>
/// <returns>True:打开连接成功,False:没有打开连接</returns>
public bool OpenConnection()
{
//如果Connection已经打开,do nothing
if(this.sqlConnection != null && (this.sqlConnection.State != ConnectionState.Closed))
{
return false;
}
//重试次数处理
//重试间隔处理
for(int numberOfRetry = 0;numberOfRetry <= connectRetryNumber;numberOfRetry++)
{
try
{
//打开连接
this.sqlConnection.Open();
}
catch(SqlException sql_Exception)
{
//休眠
System.Threading.Thread.Sleep(connectRetryInterval);
if(numberOfRetry == connectRetryNumber)
{
throw sql_Exception;
}
else
{
continue;
}
}
return true;
}
return false;
}

/// <summary>
/// 打开连接
/// </summary>
/// <param name="dsEntity">输入输出数据保存实体</param>
/// <returns>True:打开连接成功,False:没有打开连接</returns>
public bool OpenConnection(DataSet dsEntity)
{
this.dsEntity = dsEntity;
//如果Connection已经打开,do nothing
if(this.sqlConnection != null && (this.sqlConnection.State != ConnectionState.Closed))
{
return false;
}
//重试次数处理
//重试间隔处理
for(int numberOfRetry = 0;numberOfRetry <= connectRetryNumber;numberOfRetry++)
{
try
{
//打开连接
this.sqlConnection.Open();
}
catch(SqlException sql_exception)
{
System.Threading.Thread.Sleep(connectRetryInterval);
if(numberOfRetry == connectRetryNumber)
{
throw sql_exception;
}
else
{
continue;
}
}
return true;
}
return false;
}

/// <summary>
/// 关闭连接
/// </summary>
public void CloseConnection()
{
if(this.sqlConnection != null && (this.sqlConnection.State != ConnectionState.Closed))
{
this.sqlConnection.Close();
}
}

/// <summary>
/// 生成数据适配器
/// </summary>
private void CreateDataAdapter()
{
if(this.sqlDataAdapter == null)
{
this.sqlDataAdapter = new SqlDataAdapter();
this.sqlDataAdapter.SelectCommand = this.sqlCommand;
this.sqlDataAdapter.SelectCommand.Connection = this.sqlConnection;
}
}

/// <summary>
/// Insert/Update/Delete命令生成
///如果Update没有被设定的话,那么从Select文自动生成Insert/Update/Delete命令。
///※自动生成机能的限制
/// 1、Dataset的Table行是从一个Table派生出来的。
/// 2、在源Table,主键是被定义的。存在一个以上的列由唯一值。
/// 3、Table名字不能使用下记情形
/// >>空格(Space)
/// >>句号(.)
/// >>关键字(引用字符)
/// >>英数字以外的文字
/// 例外:如“dbo.table1”,这里的句号(.)用来修饰Table是可以的
/// </summary>
private void BuildCommand()
{

//DataAdapter未生成后者Select命令未生成的情况下,命令不能自动生成,do nothing。
if(this.sqlDataAdapter == null || this.sqlDataAdapter.SelectCommand == null)
{
return;
}

//如果有一个Command没有被设定,更新各Command(Insert/Update/Delete)
if(this.sqlDataAdapter.SelectCommand == null ||
this.sqlDataAdapter.InsertCommand == null ||
this.sqlDataAdapter.DeleteCommand == null)
{
//生成CommandBuiler
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(this.sqlDataAdapter);
if(this.sqlDataAdapter.InsertCommand == null)
{
this.sqlDataAdapter.InsertCommand = commandBuilder.GetInsertCommand();
}
if(this.sqlDataAdapter.UpdateCommand == null)
{
this.sqlDataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand();
}
if(this.sqlDataAdapter.DeleteCommand == null)
{
this.sqlDataAdapter.DeleteCommand = commandBuilder.GetDeleteCommand();
}
}
}

/// <summary>
/// 开始事务处理(Transaction开始)
/// </summary>
public void BeginTransaction()
{
//Insert/Update/Delete命令自动生成(如果没有)
//在事务处理中(Transaction中)不能自动生成
this.BuildCommand();
//如果连接没打开,就马上打开
this.isConnectionOpenAtTransaction = this.OpenConnection();
//Transaction开始
this.sqlTransaction = this.sqlConnection.BeginTransaction();
//设置Command.Transaction属性
if(this.sqlCommand != null)
{
if(this.sqlCommand.Transaction == null)
{
this.sqlCommand.Transaction = this.sqlTransaction;
}
else
{
if(!this.sqlCommand.Transaction.Equals(this.sqlTransaction))
{
this.sqlCommand.Transaction = this.sqlTransaction;
}
}
}
}

/// <summary>
/// 开始事务处理(Transaction开始)
/// </summary>
/// <param name="isolationlevel">事务连接的锁定级别</param>
public void BeginTransaction(System.Data.IsolationLevel isolationlevel)
{
//Insert/Update/Delete命令自动生成(如果没有)
//在事务处理中(Transaction中)不能自动生成
this.BuildCommand();
//如果连接没打开,就马上打开
this.isConnectionOpenAtTransaction = this.OpenConnection();
//Transaction开始
this.sqlTransaction = this.sqlConnection.BeginTransaction(isolationlevel);
//设置Command.Transaction属性
if(this.sqlCommand != null)
{
if(this.sqlCommand.Transaction == null)
{
this.sqlCommand.Transaction = this.sqlTransaction;
}
else
{
if(!this.sqlCommand.Transaction.Equals(this.sqlTransaction))
{
this.sqlCommand.Transaction = this.sqlTransaction;
}
}
}
}

/// <summary>
/// 提交数据库事务(Commit)
/// </summary>
public void Commit()
{
//如果Transaction没有,do nothing
if(this.sqlTransaction == null)
{
return;
}
this.sqlTransaction.Commit();
this.sqlTransaction = null;
//如果是在Transaction开始时打开的连接,那么现在关闭。
if(this.isConnectionOpenAtTransaction == true)
{
this.CloseConnection();
this.isConnectionOpenAtTransaction = false;
}
}

/// <summary>
/// 事务回滚(Rollback)
/// </summary>
public void Rollback()
{
//如果Transaction没有,do nothing
if(this.sqlTransaction == null)
{
return;
}
this.sqlTransaction.Rollback();
this.sqlTransaction = null;
//如果是在Transaction开始时打开的连接,那么现在关闭。
if(this.isConnectionOpenAtTransaction == true)
{
this.CloseConnection();
this.isConnectionOpenAtTransaction = false;
}
}

/// <summary>
/// 命令设定
/// </summary>
/// <param name="commandText">命令文本</param>
protected void SetCommand(string commandText)
{
//清空已经加载的命令参数
this.sqlCommand.Parameters.Clear();
//设定命令文本
this.sqlCommand.CommandText = commandText;
//Command类型设定
if(this.CommandText.ToUpper().StartsWith("SELECT ") ||
this.CommandText.ToUpper().StartsWith("INSERT ") ||
this.CommandText.ToUpper().StartsWith("UPDATE ") ||
this.CommandText.ToUpper().StartsWith("DELETE ") ||
this.CommandText.ToUpper().StartsWith("EXEC "))
{
//文本类型Text
this.sqlCommand.CommandType = CommandType.Text;
}
else
{
//存储过程
this.sqlCommand.CommandText = CommandType.StoredProcedure.ToString();;
}
}

/// <summary>
/// 命令设定
/// </summary>
/// <param name="commandText">命令文本</param>
/// <param name="commandType">命令类型</param>
protected void SetCommand(string commandText,CommandType commandType)
{
//清空已经加载的命令参数
this.sqlCommand.Parameters.Clear();
//设定命令文本
this.sqlCommand.CommandText = commandText;
//设定命令类型
this.sqlCommand .CommandType = commandType;
}

/// <summary>
/// 加载命令参数
/// </summary>
/// <param name="parameterName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
/// <param name="size">大小</param>
/// <param name="sourceColumn">Source列</param>
protected void AddParam(string parameterName,SqlDbType sqlDbType,
int size,string sourceColumn)
{
//加载参数
this.sqlCommand.Parameters.Add(parameterName,sqlDbType,size,sourceColumn);
}

/// <summary>
/// 加载命令参数
/// 参数方向是Input
/// </summary>
/// <param name="parameterName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
/// <param name="size">大小</param>
protected void AddParam(string parameterName,SqlDbType sqlDbType,int size)
{
//加载参数
this.sqlCommand.Parameters.Add(parameterName,sqlDbType,size);
}

/// <summary>
/// 加载命令参数
/// 参数方向是Input,值是Null
/// </summary>
/// <param name="parameterName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
protected void AddParam(string parameterName,SqlDbType sqlDbType)
{
//加载参数
this.sqlCommand.Parameters.Add(parameterName,sqlDbType);
}

/// <summary>
/// 加载命令参数
/// 参数方向是Input
/// </summary>
/// <param name="parameterName">参数名</param>
/// <param name="Value">参数值</param>
protected void AddParam(string parameterName,object Value)
{
//加载参数
this.sqlCommand.Parameters.Add(parameterName,Value);
}

/// <summary>
/// 命令参数 方向设定
/// </summary>
/// <param name="strParamName">参数名</param>
/// <param name="parameterDirection">方向</param>
protected void SetParamDirection(string strParamName,ParameterDirection parameterDirection)
{
if(this.sqlCommand.Parameters[strParamName] != null)
{
this.sqlCommand.Parameters[strParamName].Direction = parameterDirection;
}
}

/// <summary>
/// 命令参数 方向设定
/// </summary>
/// <param name="index">参数索引</param>
/// <param name="parameterDirection">方向</param>
protected void SetParamDirection(int index,ParameterDirection parameterDirection)
{
if(this.sqlCommand.Parameters.Count >= index)
{
this.sqlCommand.Parameters[index].Direction = parameterDirection;
}
}

/// <summary>
/// 参数 值设定
/// </summary>
/// <param name="strParamName">参数名</param>
/// <param name="Value">值</param>
protected void SetParamValue(string strParamName,object Value)
{
if(this.sqlCommand.Parameters[strParamName] != null)
{
this.sqlCommand.Parameters[strParamName].Value = Value;
}
}

/// <summary>
/// 参数 值设定
/// </summary>
/// <param name="index">参数索引</param>
/// <param name="Value">值</param>
protected void SetParamValue(int index,object Value)
{
if(this.sqlCommand.Parameters.Count >= index)
{
this.sqlCommand.Parameters[index].Value = Value;
}
}

/// <summary>
/// 取得参数值
/// </summary>
/// <param name="strParamName">参数名</param>
/// <returns>参数值</returns>
protected object GetParamValue(string strParamName)
{
if(this.sqlCommand.Parameters[strParamName] != null &&
this.sqlCommand.Parameters[strParamName].Value != DBNull.Value)
{
return this.sqlCommand.Parameters[strParamName].Value;
}
else
{
return null;
}
}

/// <summary>
/// 取得参数值
/// </summary>
/// <param name="index">参数索引</param>
/// <returns>参数值</returns>
protected object GetParamValue(int index)
{
if(this.sqlCommand.Parameters.Count >= index &&
this.sqlCommand.Parameters[index].Value != DBNull.Value)
{
return this.sqlCommand.Parameters[index].Value;
}
else
{
return null;
}
}

/// <summary>
/// Update命令设定
/// </summary>
/// <param name="commandText">命令文本</param>
protected void SetCommandUpdate(string commandText)
{
//如果没有数据适配器,那么生成
this.CreateDataAdapter();

//Update命令生成
this.sqlDataAdapter.UpdateCommand = new SqlCommand(commandText,this.sqlConnection);

}

/// <summary>
/// 加载Update命令参数
/// 在使用Update()时,参数和DataSet内列名对应
/// 不打算更新的Paramter的sourceVersion指定未Original
/// </summary>
/// <param name="strParamName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
/// <param name="size">大小</param>
/// <param name="strSourceColumn">源(Source)列</param>
/// <param name="sourceVersion">在读入时指定的DataRowVersion</param>
protected void AddParamUpdate(string strParamName,SqlDbType sqlDbType,int iSize,string strSourceColumn,DataRowVersion sourceVersion)
{
SqlParameter param = this.sqlDataAdapter.UpdateCommand.Parameters.Add(strParamName,sqlDbType,iSize,strSourceColumn);
param.SourceVersion = sourceVersion;
}

/// <summary>
/// Insert命令设定
/// </summary>
/// <param name="commandText">命令文本</param>
protected void SetCommandInsert(string commandText)
{
//如果没有数据适配器,那么生成
this.CreateDataAdapter();
//Insert命令生成
this.sqlDataAdapter.InsertCommand = new SqlCommand(commandText,this.sqlConnection);
}

/// <summary>
/// 加载Insert命令参数
/// 在使用Update()时,参数和DataSet内列名对应
/// </summary>
/// <param name="strParamName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
/// <param name="size">大小</param>
/// <param name="strSourceColumn">源(Source)列</param>
protected void AddParamInsert(string strParamName,SqlDbType sqlDbType,int iSize,string strSourceColumn)
{
this.sqlDataAdapter.InsertCommand.Parameters.Add(strParamName,sqlDbType,iSize,strSourceColumn);
}

/// <summary>
/// Delete命令设定
/// </summary>
/// <param name="commandText">命令文本</param>
protected void AddParamDatele(string commandText)
{
//如果没有数据适配器,那么生成
this.CreateDataAdapter();
//Insert命令生成
this.sqlDataAdapter.DeleteCommand = new SqlCommand(commandText,this.sqlConnection);
}

/// <summary>
/// 加载Delete命令参数
/// 在使用Update()时,参数和DataSet内列名对应
/// </summary>
/// <param name="strParamName">参数名</param>
/// <param name="sqlDbType">DB类型</param>
/// <param name="size">大小</param>
/// <param name="strSourceColumn">源(Source)列</param>
protected void AddParamDelete(string strParamName,SqlDbType sqlDbType,int iSize,string strSourceColumn)
{
this.sqlDataAdapter.DeleteCommand.Parameters.Add(strParamName,sqlDbType,iSize,strSourceColumn);
}

/// <summary>
/// ExcuteReader
/// </summary>
/// <param name="behavior">命令执行时的行为</param>
/// <returns>命令返回值(DataReader)</returns>
protected SqlDataReader ExecuteReader(CommandBehavior behavior)
{
//ExecuteReader自己Open,不必Close。
//因为DataReader是连接型,关闭后不能读取数据。
SqlDataReader dataReader = null;
try
{
//输出log4net日志文件
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteReader Start -- " + this.sqlCommand.CommandText);
}
dataReader = this.sqlCommand.ExecuteReader(behavior);
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteReader End");
}
}
return dataReader;
}

/// <summary>
/// ExecuteReader
/// </summary>
/// <returns>命令返回值(Datareader)</returns>
protected SqlDataReader ExecuteReader()
{
return this.sqlCommand.ExecuteReader(CommandBehavior.Default);
}

/// <summary>
/// ExecuteScalar
/// </summary>
/// <returns>命令返回值</returns>
protected object ExecuteScalar()
{
object cell = null;
try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteSaclar Start -- " + this.sqlCommand.CommandText);
}
cell = this.sqlCommand.ExecuteScalar();
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteScaler End");
}
}
return cell;
}

/// <summary>
/// ExecuteNonQuery
/// Command.ExecuteNonQuery的覆盖函数
/// </summary>
/// <returns>影响函数</returns>
protected int ExecuteNonQuery()
{
int affectedRowNum = 0;

try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteNonQuery Start -- " + this.sqlCommand.CommandText);
}
affectedRowNum = this.sqlCommand.ExecuteNonQuery();
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteNonQuery End");
}
}
return affectedRowNum;
}

/// <summary>
/// ExecuteNonQueryWithSkip
/// Command.ExecuteNonQuery的覆盖函数
/// </summary>
/// <returns>影响行数</returns>
protected int ExecuteNonQueryWithSkip()
{
int affectedRowNum = 0;
try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteNonQueryWithSkip Start -- " + this.sqlCommand.CommandText);
}
affectedRowNum = this.sqlCommand.ExecuteNonQuery();
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB ExecuteNonQueryWithSkip End");
}
}
return affectedRowNum;
}

/// <summary>
/// 填充DataSet Fill
/// SqlDataAdapter.Fill()的覆盖函数
/// </summary>
/// <param name="dataSet">将要填充的DataSet</param>
/// <param name="startRecord">开始行索引</param>
/// <param name="maxRecords">最大行数</param>
/// <param name="srcTable">填充Table名</param>
/// <returns>正常追加或更新的行数</returns>
protected int Fill(DataSet dataSet,int startRecord,int maxRecords,string srcTable)
{
this.CreateDataAdapter();
int rowNum = 0;
try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill Start -- " + this.sqlCommand.CommandText);
}
rowNum = this.sqlDataAdapter.Fill(dataSet,startRecord,maxRecords,srcTable);
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill End");
}
}
return rowNum;
}

/// <summary>
/// 填充DataSet Fill
/// SqlDataAdapter.Fill()的覆盖函数
/// </summary>
/// <param name="dataSet">将要填充的DataSet</param>
/// <param name="srcTable">填充Table名</param>
/// <returns>正常追加或更新的行数</returns>
protected int Fill(DataSet dataSet,string srcTable)
{
return this.Fill(dataSet,0,0x7FFFFFFF,srcTable);
}

/// <summary>
/// 填充DataSet Fill
/// SqlDataAdapter.Fill()的覆盖函数
/// </summary>
/// <param name="dataSet">将要填充的DataSet</param>
/// <returns>正常追加或更新的行数</returns>
protected int Fill(DataSet dataSet)
{
this.CreateDataAdapter();
int rowNum = 0;
try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill Start -- " + this.sqlCommand.CommandText);
}
rowNum = this.sqlDataAdapter.Fill(dataSet);
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill End");
}
}
return rowNum;
}

/// <summary>
/// 填充DataTable Fill
/// </summary>
/// <param name="dataTable">>将要填充的Table</param>
/// <returns>正常追加或更新的行数</returns>
protected int Fill(DataTable dataTable)
{
this.CreateDataAdapter();
int rowNum;
try
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill Start -- " + this.sqlCommand.CommandText);
}
rowNum = this.sqlDataAdapter.Fill(dataTable);
}
catch(SqlException sqlexception)
{
throw sqlexception;
}
catch(Exception exception)
{
throw exception;
}
finally
{
if(isLogOutput == true)
{
Logger.Debug(dsEntity,"DB Fill End");
}
}
return rowNum;
}

/// <summary>
/// Update
/// SqlDataAdapter.Update()的覆盖函数
/// 反映DataSet内容到DB
/// 通过使用DataSet追加、删除、变更Record,全部使用Update方法
/// </summary>
/// <param name="dataSet">数据集</param>
/// <param name="srcTable">被填充的Table名</param>
/// <returns>正常追加/更新的行数</returns>
protected int Update(DataSet dataSet,string srcTable)
{
int rowNum = 0;
if(this.sqlDataAdapter == null)
{
return rowNum;
}
this.BuildCommand();
if(this.sqlTransaction == null)
{
this.sqlDataAdapter.UpdateCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.InsertCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.DeleteCommand.Transaction = this.sqlTransaction;
}
try
{
Logger.Debug(dsEntity,"DB Update Start --" + this.sqlCommand.CommandText);
rowNum = this.sqlDataAdapter.Update(dataSet,srcTable);
}
catch(SqlException sqlException)
{
throw sqlException;
}
catch(Exception exception)
{
throw exception;
}
finally
{
Logger.Debug(dsEntity,"DB Update End");
}
return rowNum;
}
/// <summary>
/// Update
/// SqlDataAdapter.Update()的覆盖函数
/// 反映DataSet内容到DB
/// 通过使用DataSet追加、删除、变更Record,全部使用Update方法
/// </summary>
/// <param name="dataTable">被填充的Table</param>
/// <returns>正常追加/更新的行数</returns>
protected int Update(DataTable dataTable)
{
int rowNum = 0;
if(this.sqlDataAdapter == null)
{
return rowNum;
}
this.BuildCommand();
if(this.sqlTransaction == null)
{
this.sqlDataAdapter.UpdateCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.InsertCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.DeleteCommand.Transaction = this.sqlTransaction;
}
try
{
Logger.Debug(dsEntity,"DB Update Start --" + this.sqlCommand.CommandText);
rowNum = this.sqlDataAdapter.Update(dataTable);
}
catch(SqlException sqlException)
{
throw sqlException;
}
catch(Exception exception)
{
throw exception;
}
finally
{
Logger.Debug(dsEntity,"DB Update End");
}
return rowNum;
}

/// <summary>
/// Update
/// SqlDataAdapter.Update()的覆盖函数
/// 反映DataSet内容到DB
/// 通过使用DataSet追加、删除、变更Record,全部使用Update方法
/// </summary>
/// <param name="dataRow">被填充的dataRow</param>
/// <returns>正常追加/更新的行数</returns>
protected int Update(DataRow[] dataRow)
{
int rowNum = 0;
if(this.sqlDataAdapter == null)
{
return rowNum;
}
this.BuildCommand();
if(this.sqlTransaction == null)
{
this.sqlDataAdapter.UpdateCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.InsertCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.DeleteCommand.Transaction = this.sqlTransaction;
}
try
{
Logger.Debug(dsEntity,"DB Update Start --" + this.sqlCommand.CommandText);
rowNum = this.sqlDataAdapter.Update(dataRow);
}
catch(SqlException sqlException)
{
throw sqlException;
}
catch(Exception exception)
{
throw exception;
}
finally
{
Logger.Debug(dsEntity,"DB Update End");
}
return rowNum;
}

/// <summary>
/// Update
/// SqlDataAdapter.Update()的覆盖函数
/// 反映DataSet内容到DB
/// 通过使用DataSet追加、删除、变更Record,全部使用Update方法
/// </summary>
/// <param name="dataSet">被填充的dataSet</param>
/// <returns>正常追加/更新的行数</returns>
protected int Update(DataSet dataSet)
{
int rowNum = 0;
if(this.sqlDataAdapter == null)
{
return rowNum;
}
this.BuildCommand();
if(this.sqlTransaction == null)
{
this.sqlDataAdapter.UpdateCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.InsertCommand.Transaction = this.sqlTransaction;
this.sqlDataAdapter.DeleteCommand.Transaction = this.sqlTransaction;
}
try
{
Logger.Debug(dsEntity,"DB Update Start --" + this.sqlCommand.CommandText);
rowNum = this.sqlDataAdapter.Update(dataSet);
}
catch(SqlException sqlException)
{
throw sqlException;
}
catch(Exception exception)
{
throw exception;
}
finally
{
Logger.Debug(dsEntity,"DB Update End");
}
return rowNum;
}
}


/// <summary>
/// DB访问异常
/// </summary>
public class DbAccessException : DataAccessException
{

/// <summary>
/// 构造函数
/// </summary>
public DbAccessException()
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="message"></param>
public DbAccessException(string message)
:base(message)
{
}

/// <summary>
/// 构造函数
/// </summary>
/// <param name="message"></param>
/// <param name="inner"></param>
public DbAccessException(string message,Exception inner)
:base(message,inner)
{
}
}
}

/* ****************************************************************************
* Copyright (C) 2006KBA All Right Reserved。*
* ****************************************************************************/

<!-- End: WebLiver.Forums.Controls.TextPost -->
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值