c# 执行事务函数代码
程序员文章站
2024-03-09 13:44:47
复制代码 代码如下:/// /// 执行多条sql语句,实现事务 /// ///
/// <summary>
/// 执行多条sql语句,实现事务
/// </summary>
/// <param name="arraysql">多条sql语句</param>
public int executrsqltran(system.collections.arraylist arraysql)
{
int itemnum;
dbopen();
sqlcommand cm = new sqlcommand();
cm.connection = scn;
sqltransaction tx = scn.begintransaction();
cm.transaction = tx;
try
{
for (int i = 0; i < arraysql.count; i++)
{
string strsql = arraysql[i].tostring();
if (strsql.trim().length > 1)
{
cm.commandtext = strsql;
cm.executenonquery();
}
}
tx.commit();
itemnum = 1;
}
catch (sqlexception e)
{
tx.rollback();
itemnum = 0;
throw new exception(e.message);
}
dbclose();
return itemnum;
}
}
复制代码 代码如下:
/// <summary>
/// 执行多条sql语句,实现事务
/// </summary>
/// <param name="arraysql">多条sql语句</param>
public int executrsqltran(system.collections.arraylist arraysql)
{
int itemnum;
dbopen();
sqlcommand cm = new sqlcommand();
cm.connection = scn;
sqltransaction tx = scn.begintransaction();
cm.transaction = tx;
try
{
for (int i = 0; i < arraysql.count; i++)
{
string strsql = arraysql[i].tostring();
if (strsql.trim().length > 1)
{
cm.commandtext = strsql;
cm.executenonquery();
}
}
tx.commit();
itemnum = 1;
}
catch (sqlexception e)
{
tx.rollback();
itemnum = 0;
throw new exception(e.message);
}
dbclose();
return itemnum;
}
}