开发框架修正重要bug,使用SqlDataAdapter.Update方法后没有关闭SQL连接
![](http://www.csframework.com/images/article-type5.gif)
![开发框架修正重要bug,使用SqlDataAdapter.Update方法后没有关闭SQL连接 开发框架修正重要bug,使用SqlDataAdapter.Update方法后没有关闭SQL连接](http://www.csframework.com/images/seo/SEO-DbDataUpdate%E6%95%B0%E6%8D%AE%E6%A8%A1%E5%9E%8B%E6%A1%86%E6%9E%B6.jpg)
解决方案:
定义_CurrentConnection 成员变量,BeginTransaction方法内实例化,然后在CommitTransaction,RollbackTransaction方法内关闭连接。
若没有使用dalBase基类,定制的源码使用SqlDataAdapter.Update方法要注意关闭连接。
修改dalBase.cs文件,新加及修改的代码参考:
C# Code:
/// <summary>
/// 当前启用的事务
/// </summary>
protected SqlTransaction _CurrentTrans = null;
/// <summary>
/// 当前事务的连接
/// </summary>
protected SqlConnection _CurrentConnection = null;
/// <summary>
/// 启用事务控制
/// </summary>
protected virtual void BeginTransaction()
{
_CurrentConnection = DataProvider.Instance.CreateConnection(_DBName);
_CurrentTrans = _CurrentConnection.BeginTransaction();
}
/// <summary>
/// 提交事务
/// </summary>
protected virtual void CommitTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Commit();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
/// <summary>
/// 事务回滚
/// </summary>
protected virtual void RollbackTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Rollback();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
/// <summary>
/// 当前启用的事务
/// </summary>
protected SqlTransaction _CurrentTrans = null;
/// <summary>
/// 当前事务的连接
/// </summary>
protected SqlConnection _CurrentConnection = null;
/// <summary>
/// 启用事务控制
/// </summary>
protected virtual void BeginTransaction()
{
_CurrentConnection = DataProvider.Instance.CreateConnection(_DBName);
_CurrentTrans = _CurrentConnection.BeginTransaction();
}
/// <summary>
/// 提交事务
/// </summary>
protected virtual void CommitTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Commit();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
/// <summary>
/// 事务回滚
/// </summary>
protected virtual void RollbackTransaction()
{
try
{
if (_CurrentTrans != null)
{
_CurrentTrans.Rollback();
_CurrentTrans = null;
DataProvider.Instance.CloseConnection(_CurrentConnection);
}
}
catch
{
DataProvider.Instance.CloseConnection(_CurrentConnection);
throw;
}
}
//来源:C/S框架网(www.csframework.com) QQ:1980854898
![C/S框架网|原创精神.创造价值.打造精品](http://www.csframework.com/images/our03.gif)
扫一扫加作者微信
![C/S框架网作者微信](http://www.csframework.com/images/wechatcode.jpg)
![C/S框架网|原创作品.质量保障.竭诚为您服务](http://www.csframework.com/images/CS框架网提供高效优质服务.jpg)
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网