求c#调用事务,存储过程的代码,各一个方法就行了!
1个回答
2013-05-16
展开全部
/// <summary>
/// 开始数据库事务
/// </summary>
/// <returns>true|false(throw ex)</returns>
public bool BeginTrans()
{
bool bolFlag = false;
try
{
if(this._SqlCon.State!=ConnectionState.Open)
{
this._SqlCon.Open();
}
this._SqlTrans = this._SqlCon.BeginTransaction();
bolFlag = true;
}
catch (Exception ex)
{
bolFlag=false;
throw ex;
}
return bolFlag;
} /// <summary>
/// 提交数据库事务
/// </summary>
/// <returns>true|false(throw ex)</returns>
public bool Commit()
{
bool bolFlag = false;
try
{
if(this._SqlTrans!=null)
{
this._SqlTrans.Commit();
}
bolFlag = true;
}
catch (Exception ex)
{
bolFlag=false;
throw ex;
}
finally
{
this._SqlTrans.Dispose();
this._SqlTrans=null;
CloseSqlCon();
}
return bolFlag;
}
/// <summary>
/// 执行带参数的存储过程 返回受影响的行数
/// </summary>
/// <param name="sSPName">存储过程名</param>
/// <param name="sParm">SqlParameter数组</param>
/// <returns>受影响的行数</returns>
public int ExecuteNonQueryBySP(string sSPName,SqlParameter[] sParm)
{
try
{
if(this._SqlCon.State!=ConnectionState.Open)
{
this._SqlCon.Open();
}
this._SqlCmd.Parameters.Clear();
this._SqlCmd.CommandType=CommandType.StoredProcedure;
this._SqlCmd.CommandText=sSPName;
foreach(SqlParameter sp in sParm)
{
this._SqlCmd.Parameters.Add(sp);
}
CheckOpenTrans();
return this._SqlCmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
finally
{
CheckCloseTrans();
}
}
/// 开始数据库事务
/// </summary>
/// <returns>true|false(throw ex)</returns>
public bool BeginTrans()
{
bool bolFlag = false;
try
{
if(this._SqlCon.State!=ConnectionState.Open)
{
this._SqlCon.Open();
}
this._SqlTrans = this._SqlCon.BeginTransaction();
bolFlag = true;
}
catch (Exception ex)
{
bolFlag=false;
throw ex;
}
return bolFlag;
} /// <summary>
/// 提交数据库事务
/// </summary>
/// <returns>true|false(throw ex)</returns>
public bool Commit()
{
bool bolFlag = false;
try
{
if(this._SqlTrans!=null)
{
this._SqlTrans.Commit();
}
bolFlag = true;
}
catch (Exception ex)
{
bolFlag=false;
throw ex;
}
finally
{
this._SqlTrans.Dispose();
this._SqlTrans=null;
CloseSqlCon();
}
return bolFlag;
}
/// <summary>
/// 执行带参数的存储过程 返回受影响的行数
/// </summary>
/// <param name="sSPName">存储过程名</param>
/// <param name="sParm">SqlParameter数组</param>
/// <returns>受影响的行数</returns>
public int ExecuteNonQueryBySP(string sSPName,SqlParameter[] sParm)
{
try
{
if(this._SqlCon.State!=ConnectionState.Open)
{
this._SqlCon.Open();
}
this._SqlCmd.Parameters.Clear();
this._SqlCmd.CommandType=CommandType.StoredProcedure;
this._SqlCmd.CommandText=sSPName;
foreach(SqlParameter sp in sParm)
{
this._SqlCmd.Parameters.Add(sp);
}
CheckOpenTrans();
return this._SqlCmd.ExecuteNonQuery();
}
catch(Exception ex)
{
throw ex;
}
finally
{
CheckCloseTrans();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询