使用三层开发时,两个连续的添加,如果第二个出现错误,如何使第一个回滚呢? 使用三层开发时,两个连续的添加,如果第二个出现错误,如何使第一个回滚呢?mainFeeDAL1.Add(mianFee1);//如果这条命令成功添加chuandaiFeeDAL1.Add(chuandaiFee1);//但是这条命令出现了错误如何使第一条回滚呢? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1. 把2个sql写在一个事务里面2. 第2条出错就把第一条记录删除。。呵呵 你可以写成Sql的存储过程啊在其中用Sql的事物 两种方式:1.使用存储过程,在里面使用事务。2.使用ADO.net事务处理机制。 事务处理,三层的话,在BLL层开启事物进行处理。简单点话,用存储过程里的事务吧 无非是事务二字。try { cmd.CommandText = "";//插入信息1 cmd.ExecuteNonQuery(); cmd.CommandText = "";//插入信息2 cmd.ExecuteNonQuery(); tran.Commit(); MessageBox.Show("事务提交成功!"); } catch(Exception ex) { tran.Rollback(); MessageBox.Show("Error!"+ex.Message); } } //建议增加一个事务层,并从现有DAL层增加若干方法,返回SqlListpublic bool ExecuteCommand(ArrayList SqlList) { DbConnection con = CreateConnection(); con.Open(); bool iserror = false; string strerror = ""; DbTransaction SqlTran = con.BeginTransaction(); try { for (int i = 0; i < SqlList.Count; i++) { DbCommand _command = GetDbProviderFactory().CreateCommand(); _command.Connection = con; _command.CommandTimeout = 500; _command.CommandText = SqlList[i].ToString(); _command.Transaction = SqlTran; _command.ExecuteNonQuery(); _command.Dispose(); } } catch (Exception ex) { iserror = true; strerror = ex.Message; } finally { if (iserror) { SqlTran.Rollback(); throw new Exception(strerror); } else { SqlTran.Commit(); } con.Close(); } if (iserror) { return false; } else { return true; } } public int ExecuteCommand(ArrayList SqlList, List<System.Data.Common.DbParameter[]> paramlist) { DbConnection con = CreateConnection(); con.Open(); bool iserror = false; string strerror = ""; int count = 0; DbTransaction SqlTran = con.BeginTransaction(); string s = ""; try { for (int i = 0; i < SqlList.Count; i++) { DbCommand _command = GetDbProviderFactory().CreateCommand(); _command.Connection = con; _command.CommandTimeout = 500; _command.CommandText = SqlList[i].ToString(); s = SqlList[i].ToString(); for (int n = 0; n < paramlist[i].Length; n++) { DbParameter p = paramlist[i][n]; if (p.Value == null || p.Value.ToString() == "") { p.Value = DBNull.Value; } _command.Parameters.Add(p); } _command.Transaction = SqlTran; count += _command.ExecuteNonQuery(); _command.Dispose(); } } catch (Exception ex) { iserror = true; strerror = ex.Message; count = 0; } finally { if (iserror) { SqlTran.Rollback(); throw new Exception(strerror); } else { SqlTran.Commit(); } con.Close(); } return count; } 关于新浪微博注册的请教 C#把打印的内容保存在word里 未处理 System.Runtime.InteropServices.COMException windows7 mysql 绝对高分求一解决方案(有了好的再加 100~200 分) 多线程 程序编译没问题,可是测试时跳出 未处理的“System.Data.OleDb.OleDbException”类型的异常出现在 system.data.dll 中 是什么意思 ?大 关于treeview控件的问题 关于dll设计 datagrid控件(winform)问题 大家好,我问个附加数据库的 请问如何用DataGridView保存数据
把2个sql写在一个事务里面2.
第2条出错就把第一条记录删除。。呵呵
在其中用Sql的事物
2.使用ADO.net事务处理机制。
事务处理,三层的话,在BLL层开启事物进行处理。简单点话,用存储过程里的事务吧
try
{
cmd.CommandText = "";//插入信息1
cmd.ExecuteNonQuery();
cmd.CommandText = "";//插入信息2
cmd.ExecuteNonQuery();
tran.Commit();
MessageBox.Show("事务提交成功!");
}
catch(Exception ex)
{
tran.Rollback();
MessageBox.Show("Error!"+ex.Message);
}
}
public bool ExecuteCommand(ArrayList SqlList)
{
DbConnection con = CreateConnection();
con.Open();
bool iserror = false;
string strerror = "";
DbTransaction SqlTran = con.BeginTransaction();
try
{
for (int i = 0; i < SqlList.Count; i++)
{ DbCommand _command = GetDbProviderFactory().CreateCommand();
_command.Connection = con;
_command.CommandTimeout = 500;
_command.CommandText = SqlList[i].ToString();
_command.Transaction = SqlTran;
_command.ExecuteNonQuery();
_command.Dispose();
} }
catch (Exception ex)
{
iserror = true;
strerror = ex.Message; }
finally
{ if (iserror)
{
SqlTran.Rollback();
throw new Exception(strerror);
}
else
{
SqlTran.Commit();
}
con.Close();
}
if (iserror)
{
return false;
}
else
{
return true;
}
} public int ExecuteCommand(ArrayList SqlList, List<System.Data.Common.DbParameter[]> paramlist)
{
DbConnection con = CreateConnection();
con.Open();
bool iserror = false;
string strerror = "";
int count = 0;
DbTransaction SqlTran = con.BeginTransaction();
string s = "";
try
{
for (int i = 0; i < SqlList.Count; i++)
{
DbCommand _command = GetDbProviderFactory().CreateCommand();
_command.Connection = con;
_command.CommandTimeout = 500;
_command.CommandText = SqlList[i].ToString();
s = SqlList[i].ToString();
for (int n = 0; n < paramlist[i].Length; n++)
{
DbParameter p = paramlist[i][n];
if (p.Value == null || p.Value.ToString() == "")
{
p.Value = DBNull.Value;
}
_command.Parameters.Add(p);
}
_command.Transaction = SqlTran;
count += _command.ExecuteNonQuery();
_command.Dispose();
} }
catch (Exception ex)
{
iserror = true;
strerror = ex.Message;
count = 0;
}
finally
{ if (iserror)
{
SqlTran.Rollback();
throw new Exception(strerror);
}
else
{
SqlTran.Commit();
}
con.Close();
}
return count;
}