Call multiple SQL Server stored procedures in a transaction

后端 未结 3 976
太阳男子
太阳男子 2020-12-31 08:23

For usage in my current project I\'ve created a class that allows me to call SQL Server async.

My code looks like this:

internal class CommandAndCall         


        
相关标签:
3条回答
  • 2020-12-31 08:49

    Yes, it is possible. Simply call SqlConnection.BeginTransaction before your first call, make you assign the returned SqlTransaction object to each SqlCommand.Transaction in the chain and call SqlTransaction.Commit() at the end.

    0 讨论(0)
  • 2020-12-31 08:56
      string cnnString =WebConfigurationManager.ConnectionStrings["MyString"].ConnectionString;
        SqlConnection cnn = new SqlConnection(cnnString);
        SqlTransaction transaction;
    
        cnn.Open();
        transaction = cnn.BeginTransaction();
    
        try
        {
    
            // Command Objects for the transaction
            SqlCommand cmd1 = new SqlCommand("sproc1", cnn);
            SqlCommand cmd2 = new SqlCommand("sproc2", cnn);
    
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd2.CommandType = CommandType.StoredProcedure;
    
            cmd1.Parameters.Add(new SqlParameter("@Param1", SqlDbType.NVarChar, 50));
            cmd1.Parameters["@Param1"].Value = paramValue1;
    
            cmd1.Parameters.Add(new SqlParameter("@Param2", SqlDbType.NVarChar, 50));
            cmd1.Parameters["@Param2"].Value = paramValue2;
    
            cmd2.Parameters.Add(new SqlParameter("@Param3", SqlDbType.NVarChar, 50));
            cmd2.Parameters["@Param3"].Value = paramValue3;
    
            cmd2.Parameters.Add(new SqlParameter("@Param4", SqlDbType.NVarChar, 50));
            cmd2.Parameters["@Param4"].Value = paramValue4;
    
            cmd1.ExecuteNonQuery();
            cmd2.ExecuteNonQuery();
    
            transaction.Commit();
        }
    
        catch (SqlException sqlEx)
        {
            transaction.Rollback();
        }
    
        finally
        {
            cnn.Close();
            cnn.Dispose();
        }
    
    0 讨论(0)
  • 2020-12-31 09:12
    public class Command
    {
        public string sql { get; set; }
        public CommandType cmdType { get; set; }
        public Dictionary<string, object> parameter { get; set; } = null;
    }
    
        private Command insertInvoice(Invoice invoice)
        {
            try
            {
                Dictionary<string, object> parameterLocal = new Dictionary<string, object>();
    
                parameterLocal.Add("p_customerId", invoice.customerId);
                parameterLocal.Add("p_invoiceNo", invoice.invoiceNo);
                parameterLocal.Add("p_invoiceDate", invoice.invoiceDate);
                parameterLocal.Add("p_invoiceAmount", invoice.invoiceAmount);                
                parameterLocal.Add("p_withInvoice", invoice.withInvoice);
    
                return (new Command { sql = "sp_insertInvoice", cmdType = CommandType.StoredProcedure, parameter = parameterLocal });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
        private Command insertInvoiceModel(InvoiceModel invoiceModel)
        {
            try
            {
                Dictionary<string, object> parameterLocal = new Dictionary<string, object>();
    
                parameterLocal.Add("p_invoiceNo", invoiceModel.invoiceNo);
                parameterLocal.Add("p_model", invoiceModel.model);
                parameterLocal.Add("p_quantity", invoiceModel.quantity);
                parameterLocal.Add("p_unitPrice", invoiceModel.unitPrice);
    
                return (new Command { sql = "sp_insertInvoiceModel", cmdType = CommandType.StoredProcedure, parameter = parameterLocal });
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    
     List<Command> commandList = new List<Command>();
    
     cmd = insertInvoice(invoicesave);
    
     commandList.Add(cmd);
    
     cmd = insertInvoiceModel(invoiceModelSave);
    
     commandList.Add(cmd);
    
            try
            {
                erplibmain.erpDac.runOleDbTransaction(commandList);
            }
            catch (Exception ex)
            {
                throw ex;
            }
    
        public void runOleDbTransaction(List<Command> commandList)
        {
            OleDbConnection erpConnection = new OleDbConnection(ErpDalMain.connectionstring);
            erpConnection.Open();
    
            OleDbCommand erpCommand = erpConnection.CreateCommand();
            OleDbTransaction erpTrans;
    
            // Start a local transaction
            erpTrans = erpConnection.BeginTransaction();
            // Assign transaction object for a pending local transaction
            erpCommand.Connection = erpConnection;
            erpCommand.Transaction = erpTrans;
    
            try
            {
                foreach (Command cmd in commandList)
                {
                    erpCommand.CommandText = cmd.sql;
                    erpCommand.CommandType = cmd.cmdType;
    
                    foreach (KeyValuePair<string, object> entry in cmd.parameter)
                    {
                        erpCommand.Parameters.AddWithValue(entry.Key, entry.Value);
                    }
    
                    erpCommand.ExecuteNonQuery();
    
                    erpCommand.Parameters.Clear();
                }
    
                erpTrans.Commit();
            }
            catch (Exception e)
            {
                try
                {
                    erpTrans.Rollback();
                }
                catch (OleDbException ex)
                {
                    if (erpTrans.Connection != null)
                    {
                        throw ex;
                    }
                }
    
                throw e;
            }
            finally
            {
                erpConnection.Close();
            }
        }
    
    0 讨论(0)
提交回复
热议问题