This is very basic ADO.NET - you need to set up your connection and transaction, and then you need three commands that "participate" in that transaction. You execute your commands, and if all goes well, you commit the transaction. If anything fails, a try...catch will kick in and roll back the transaction to the state before this all started.
Code will look something like this:
// set up your connection
using (SqlConnection conn = new SqlConnection("--your-connection-string-here--"))
{
// start a transaction
using (SqlTransaction transaction = conn.BeginTransaction())
{
try
{
conn.Open();
// create a command for your first stored procedure
// and make sure it uses the transaction from above
using (SqlCommand cmdProc1 = new SqlCommand("dbo.Procedure1Name", conn, transaction))
{
// set the parameters
cmdProc1.Parameters.Add("@ParamA", SqlDbType.Int).Value = a;
cmdProc1.Parameters.Add("@ParamB", SqlDbType.Int).Value = b;
cmdProc1.Parameters.Add("@ParamC", SqlDbType.Int).Value = c;
// execute stored procedure
cmdProc1.ExecuteNonQuery();
}
// create a command for your second stored procedure
// and make sure it uses the transaction from above
using (SqlCommand cmdProc2 = new SqlCommand("dbo.Procedure2Name", conn, transaction))
{
// set the parameters
cmdProc2.Parameters.Add("@ParamI", SqlDbType.Int);
cmdProc2.Parameters.Add("@ParamB", SqlDbType.Int);
cmdProc2.Parameters.Add("@ParamC", SqlDbType.Int);
// loop and set parameter values
for (int i = 0; i < gridview1.rows.count; i++)
{
cmdProc2.Parameters["@ParamI"].Value = i;
cmdProc2.Parameters["@ParamB"].Value = b;
cmdProc2.Parameters["@ParamC"].Value = c;
cmdProc2.ExecuteNonQuery();
}
}
// create a command for your third stored procedure
// and make sure it uses the transaction from above
using (SqlCommand cmdProc3 = new SqlCommand("dbo.Procedure3Name", conn, transaction))
{
// set the parameters
cmdProc3.Parameters.Add("@ParamA", SqlDbType.Int).Value = a;
cmdProc3.Parameters.Add("@ParamB", SqlDbType.Int).Value = b;
cmdProc3.Parameters.Add("@ParamC", SqlDbType.Int).Value = c;
// execute stored procedure
cmdProc3.ExecuteNonQuery();
}
// if everything went well - commit the transaction!
transaction.Commit();
}
catch (Exception exc)
{
// log the exception, rollback the transaction
transaction.Rollback();
}
}
}