Reputation: 43778
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 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#?
Upvotes: 1
Views: 345
Reputation: 6637
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.
Upvotes: 1