往3张表插入数据,使用事物
现要往3张表插入数据,想使用事物,该怎么写??
我传递的参是 model
------解决方案--------------------可以考虑TransactionScope
------解决方案--------------------在后台执行sql增删改前开启事物啊
------解决方案--------------------http://www.cnblogs.com/Garden-blog/archive/2011/04/21/2023417.html
------解决方案-------------------- /// <summary>
/// 带事务的批量操作数据。
/// </summary>
/// <param name="sqlInsert">SQL</param>
/// <returns>操作是否成功</returns>
public static bool OperateDatasWithTransaction(List<string> sqlList)
{
OracleConnection myConnection = new OracleConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnStrOrcl"]);
myConnection.Open();
OracleCommand myCommand = myConnection.CreateCommand();
OracleTransaction myTrans;
// Start a local transaction
myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted);
// Assign transaction object for a pending local transaction
myCommand.Connection = myConnection;
myCommand.Transaction = myTrans;
try
{
foreach (string item in sqlList)
{
// myCommand.CommandText = item;
myCommand.ExecuteNonQuery();
}
myTrans.Commit();
return true;
}
catch (Exception err)
{
WriteLog(err.ToString());
myTrans.Rollback();
return false;
}
finally
{
myConnection.Close();
}
}
#endregion
------解决方案--------------------自己写存储过程,这才是好办法,
------解决方案-------------------- /// <summary>
/// 执行事务(同一数据库)
/// </summary>
/// <param name="param">结构体,里面是sql语句和参数</param>
/// <returns></returns>
public static bool ExecuteTrans(string dbName,SqlParams[] param)
{
bool result = false;
SqlConnection conn = dbManager.GetConn(dbName);
if (conn == null)
{
return result;
}
SqlTransaction trans = conn.BeginTransaction();
using (SqlCommand cmd = new SqlCommand())
{
try
{
cmd.Transaction = trans;
int rows = -1;
foreach (SqlParams item in param)
{
//HttpContext.Current.Response.Write("<br>");
//HttpContext.Current.Response.Write(item.sqlstring);
//HttpContext.Current.Response.Flush();
if (item.sqlstring != ""&&item.sqlstring!=null)
{
DbHelperSQL.PrepareCommand(cmd, conn, null, item.sqlstring, item.parameters);
//DbHelperSQL.ExecuteSql(item.sqlstring, conn, item.parameters);
rows = cmd.ExecuteNonQuery();
}
}
trans.Commit();
result = true;