locked
multiple sp how to using c# RRS feed

  • Question

  • how to use multiple stored procedures within a single transaction using c#

    • Moved by Bob Beauchemin Thursday, January 24, 2013 5:27 PM Moved to forum for SqlClient questions for best results
    Thursday, January 24, 2013 1:32 PM

Answers

  • Simple example:

                using (var connection = new SqlConnection(connectionString))
                {
                    using (SqlCommand proc1 = new SqlCommand("dbo.proc1", connection)
                        , proc2 = new SqlCommand("dbo.proc2", connection))
                    {
                        var tran = connection.BeginTransaction();
    
                        proc1.CommandType = CommandType.StoredProcedure;
                        proc1.Transaction = tran;
                        proc1.ExecuteNonQuery();
    
                        proc2.CommandType = CommandType.StoredProcedure;
                        proc2.Transaction = tran;
                        proc2.ExecuteNonQuery();
    
                        tran.Commit();
                    }
                }


    Dan Guzman, SQL Server MVP, http://www.dbdelta.com

    • Marked as answer by Alexander Sun Wednesday, January 30, 2013 8:58 AM
    Thursday, January 24, 2013 1:48 PM