如何在使用存储过程时在asp.net中执行回滚

时间:2015-05-26 05:08:20

标签: asp.net sql-server-2008

我想通过单击一个按钮将数据插入12个不同的表中。为此,我使用单个存储过程。但我的问题是当我这样做,如果有任何异常发生我的数据部分插入,但值被插入一些表,一些仍然是空的,由于这个问题发生,因为所有都相互关联。所以想知道有没有办法执行回滚,这样如果发生任何异常,整个查询将被回滚,数据不会插入任何表中。 这是我目前用于插入值的代码。

public int Sp_InsertUpdateDelete(string s, SqlParameter[] spa)
{
    SqlConnection sc = new SqlConnection(cs);
    sc.Open();
    SqlCommand scm = new SqlCommand(s, sc);
    scm.CommandType = CommandType.StoredProcedure;
    foreach (SqlParameter sql in spa)
    {
        scm.Parameters.Add(sql);
    }
    int k = scm.ExecuteNonQuery();
    sc.Close();
    return k;

}



protected void btnHostingSubmit_Click(object sender, EventArgs e)
{
    string select = "select * from tbl_Hosting where Customer_Id='" + ddlCustomerName.SelectedValue + "'";
    DataSet s = gs.select(select);
    if (s.Tables[0].Rows.Count > 0)
    {
        Response.Write("<script>alert('Customer Already Exist');</script>");
    }
    else
    {
        if (ddlHosting.SelectedValue == "Yes")
        {
            SqlParameter[] spa = new SqlParameter[29];
            spa[0] = new SqlParameter("@Customer_Id", Convert.ToInt16(ddlCustomerName.SelectedValue));
            spa[1] = new SqlParameter("@Type", 2);
            //Hosting
            if (txtHostingSDate.Text == "" || txtHostingSDate.Text == null)
            {
                spa[2] = new SqlParameter("@Hosting_start_date", null);
            }
            else
            {
                spa[2] = new SqlParameter("@Hosting_start_date", Convert.ToDateTime(txtHostingSDate.Text));
            }
            if (txtHosingEDate.Text == "" || txtHosingEDate.Text == null)
            {
                spa[3] = new SqlParameter("@Hosting_end_date", null);
            }
            else
            {
                spa[3] = new SqlParameter("@Hosting_end_date", Convert.ToDateTime(txtHosingEDate.Text));
            }
            spa[4] = new SqlParameter("@Hosting_provider", ddlHostingPro.SelectedItem.ToString());
            spa[5] = new SqlParameter("@Hosting_type", ddlHostingType.SelectedItem.ToString());
            spa[6] = new SqlParameter("@Hosting_server", ddlHostingServer.SelectedItem.ToString());
            spa[7] = new SqlParameter("@Hosting_total_id", Convert.ToInt16(txtHostingId.Text));
            spa[8] = new SqlParameter("@Hosting_mail_tracking", ddlHostingMailTracking.SelectedItem.ToString());
            spa[9] = new SqlParameter("@Hosting_mail_tracking_users", Convert.ToInt16(txtHostingMtUser.Text));
            spa[10] = new SqlParameter("@Hosting_dns", ddlHostingDns.SelectedItem.ToString());
            spa[11] = new SqlParameter("@Hosting_mail", ddlHostingMail.SelectedItem.ToString());
            spa[12] = new SqlParameter("@Hosting_web", ddlHostingWeb.SelectedItem.ToString());
            spa[13] = new SqlParameter("@Hosting_manage_dns", ddlHostingMngDns.SelectedItem.ToString());

            if (ddlHostingDns.SelectedValue == "No" && (ddlHostingMail.SelectedValue == "Yes" || ddlHostingWeb.SelectedValue == "Yes"))
            {
                spa[14] = new SqlParameter("@Hosting_ns1", txtNS1.Text);
                spa[15] = new SqlParameter("@Hosting_ns2", txtNS2.Text);
            }
            else
            {
                spa[14] = new SqlParameter("@Hosting_ns1", ddlHostingNS1.SelectedItem.ToString());
                spa[15] = new SqlParameter("@Hosting_ns2", ddlHostingNS2.SelectedItem.ToString());
            }


            spa[16] = new SqlParameter("@Hosting_rec_ip", txtHostingARecordIp.Text);
            spa[17] = new SqlParameter("@Hosting_mx_rec1", txtMXRecord1.Text);
            spa[18] = new SqlParameter("@Hosting_mx_rec2", txtMXRecord2.Text);
            spa[19] = new SqlParameter("@Hosting_mx_ip1", txtHostingMxIp1.Text);
            spa[20] = new SqlParameter("@Hosting_space", ddlHostingSpace.SelectedItem.ToString());
            spa[21] = new SqlParameter("@Hosting_mx_ip2", txtHostingMxIp2.Text);
            spa[22] = new SqlParameter("@Hosting_data_transfer", ddlhostingDataTrans.SelectedItem.ToString());
            spa[23] = new SqlParameter("@Hosting_manage_dns_amt", txtHostingMangDnsAmt0.Text);
            spa[24] = new SqlParameter("@Hosting_amt", txtHostingAmt0.Text);
            spa[25] = new SqlParameter("@Hosting_c_ns1", txtHostingNS1.Text);
            spa[26] = new SqlParameter("@Hosting_c_ns2", txtHostingNS2.Text);
            spa[27] = new SqlParameter("@Hosting_c_ns3", txtHostingNS3.Text);
            spa[28] = new SqlParameter("@Hosting_c_ns4", txtHostingNS4.Text); 
int k = gs.Sp_InsertUpdateDelete("Sp_Hosting", spa);  
if (k > 0)
            {
                Response.Write("<script>alert('Hosting Added Success');</script>");
            }
            Clear();

        }

2 个答案:

答案 0 :(得分:1)

using(SqlConnection conn = new SqlConnection())
{
   try
   {
    conn.Open();

    SqlTransaction tran = conn.BeginTransaction("Transaction1");
Cmd = new SqlCommand(sQuery, Conn);
Cmd.Transaction = tran;
    //Your Code

    tran.Commit(); //both are successful
   }
   catch(Exception ex)
   {
      //if error occurred, reverse all actions. By this, your data consistent and correct
      tran.Rollback();
   }
}

https://msdn.microsoft.com/en-us/library/a90c30fy.aspx

答案 1 :(得分:0)

您需要修改存储过程并使用此功能进行交易。

这样的事情:

&#13;
&#13;
DECLARE @TranName VARCHAR(20);
SELECT @TranName = 'MyTransaction';

BEGIN TRANSACTION @TranName;
USE AdventureWorks2012;
DELETE FROM AdventureWorks2012.HumanResources.JobCandidate
    WHERE JobCandidateID = 13;

COMMIT TRANSACTION @TranName;
GO
&#13;
&#13;
&#13;

MSDN Reference