开发者

try..catch then rollback the already run stored prod in .net c# web application

Does anyone know, how can I rollback the already run stored prod inside try...catch in .net c#?

Example:

If I have 2 stored proc need to be run, 2 function is created 开发者_C百科to run each of the stored prod, and those function will be call inside the try...catch, the 1st stored prod run successfully, however the 2nd stored prod having an error and timeout. In this case, any way I can rollback the 1st stored prod in my .net c#?


Have a look at

private static void DemoFunc() 
   {
      SqlConnection conn = new SqlConnection("");//conection string here
      SqlTransaction transaction;
      SqlCommand cmd;

      conn.Open();
      transaction = conn.BeginTransaction();
      try 
      {
         cmd = new SqlCommand("MySP1", conn, transaction);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.ExecuteNonQuery();

         cmd = new SqlCommand("MySP2", conn, transaction);
         cmd.CommandType = CommandType.StoredProcedure;
         cmd.ExecuteNonQuery();
         transaction.Commit();
      } 
      catch (SqlException sqlError) 
      {
         transaction.Rollback();
      }
      conn.Close();
   }

Hope this help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜