急!!!!!!以下代码rollback()有什么含义?
try
{
myCommand.CommandText = \ "Insert into Region (RegionID, RegionDescription) VALUES (100, \ 'Description\ ')\ ";
myCommand.ExecuteNonQuery();
myCommand.CommandText = \ "Insert into Region (RegionID, RegionDescription) VALUES (101, \ 'Description\ ')\ ";
myCommand.ExecuteNonQuery();
myTrans.Commit();
}
catch(Exception e)
{
try
{
myTrans.Rollback();
}
catch (OleDbException ex)
{}
}
finally
{
myConnection.Close();
}
这段代码有2条插入语句
如果有一条出错会产生异常,程序就跳到catch块里面去
不会执行commit()
这样的话有没有rollback()语句
效果都是一样的
数据库里面没有插入记录
那还要rollback()做什么?
------解决方案--------------------catch(Exception eX)
{
trans.Rollback();
throw eX;
}
个人认为不要在cahtch里面再嵌套try-catch了!
------解决方案--------------------个人认为不要在cahtch里面再嵌套try-catch了!
--
支持
------解决方案-------------------- catch(Exception eX)
{
trans.Rollback();
throw eX;
}
个人认为不要在cahtch里面再嵌套try-catch了!
------------------------------------
这样写会隐藏真实的exception
catch
{
trans.Rollback();
throw;
}
------解决方案--------------------呵呵 正解
官方写法
catch(Exception e)
{
try
{
myTrans.Rollback( "SampleTransaction ");
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine( "An exception of type " + ex.GetType() +
" was encountered while attempting to roll back the transaction. ");
}
}
Console.WriteLine( "An exception of type " + e.GetType() +
" was encountered while inserting the data. ");
Console.WriteLine( "Neither record was written to database. ");
}
------解决方案--------------------catch(Exception eX)
{
trans.Rollback();
throw eX;
}
个人认为不要在cahtch里面再嵌套try-catch了!
------------------------------------
这样写会隐藏真实的exception
catch
{
trans.Rollback();
throw;
}
--------------------------
你第2种当然会隐藏,但是我的写法
catch(Exception eX)
{
trans.Rollback();
throw eX;
}
是不会的.你理解有误,看清楚我的参数