Monday, September 21, 2009

//*// Transaction in sql with c# //*//

public int DeleteProblems(int _intProblemId)
{
int icheck = 0;
using (SqlConnection connection = new SqlConnection(_strConnectionString))
{
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction = null;

try
{
// BeginTransaction() Requires Open Connection
connection.Open();

transaction = connection.BeginTransaction();

// Assign Transaction to Command
command.Transaction = transaction;

// Execute 1st Command
command.CommandText = “delete from ForumSolution where ProblemId=’” + _intProblemId + “‘”;
icheck=command.ExecuteNonQuery();

// Execute 2nd Command
command.CommandText = “delete from ForumProblem where ProblemId=’” + _intProblemId + “‘”;
icheck=command.ExecuteNonQuery();

transaction.Commit();
}
catch
{
transaction.Rollback();
throw;
}
finally
{
connection.Close();
}
return icheck;
}

}

No comments:

Post a Comment