如果下一个表事务失败,如何删除上一个表事务

本文关键字:事务 删除 上一个 下一个 失败 如果 何删除 | 更新日期: 2023-09-27 17:53:30

我将一些数据一个接一个地插入到表中。我有两个表,adjustment_headeradjustment_grid

首先我将插入数据到adjustment_header表,然后我将插入数据到adjustment_grid表。如果插入调整失败,adjustment_header表中先前插入的数据将被自动删除。

这类问题有查询吗?

SqlCommand sqlcmd1 = new SqlCommand("INSERT INTO adjustment_header values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')",conn);
conn.Open();
sqlcmd1.ExecuteNonQuery();
conn.Close();
//adjustment grid row 1
if (itemno1.SelectedItem.Text != "please select")
{
    SqlCommand cmd1 = new SqlCommand("INSERT INTO adjustment_grid values('"+TextBox1.Text+"','" + itemno1.SelectedItem.Text + "','" + adj1.SelectedItem.Text + "','" + store1.SelectedItem.Text + "','" + qty1.Text + "','" + cost1.Text + "')", conn);
    conn.Open();
    cmd1.ExecuteNonQuery();
    conn.Close();
}
//adjustment grid row 2
if (itemno2.SelectedItem.Text != "please select")
{
    SqlCommand cmd2 = new SqlCommand("INSERT INTO adjustment_grid values('" + TextBox1.Text + "','" + itemno2.SelectedItem.Text + "','" + adj2.SelectedItem.Text + "','" + store2.SelectedItem.Text + "','" + qty2.Text + "','" + cost2.Text + "')", conn);
    conn.Open();
    cmd2.ExecuteNonQuery();
    conn.Close();
}
//adjustment grid row 3
if (itemno3.SelectedItem.Text != "please select")
{
    SqlCommand cmd3 = new SqlCommand("INSERT INTO adjustment_grid values('" + TextBox1.Text + "','" + itemno3.SelectedItem.Text + "','" + adj3.SelectedItem.Text + "','" + store3.SelectedItem.Text + "','" + qty3.Text + "','" + cost3.Text + "')", conn);
    conn.Open();
    cmd3.ExecuteNonQuery();
    conn.Close();
}

在这段代码中,我首先将数据插入adjustment_header表,然后我插入adjustment_grid表3次,在adjustment_grid表的3个事务中,任何一个失败,以前插入的数据应该自动删除。

如果下一个表事务失败,如何删除上一个表事务

SqlTransaction中包装整个块,并且不要为每个语句打开/关闭连接:

conn.Open();
using(SqlTransaction tran = conn.BeginTransaction("Adjustment"))
{
    SqlCommand sqlcmd1 = new SqlCommand("INSERT INTO adjustment_header values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')",conn, tran);
    sqlcmd1.ExecuteNonQuery();
    //adjustment grid row 1
    if (itemno1.SelectedItem.Text != "please select")
    {
        SqlCommand cmd1 = new SqlCommand("INSERT INTO adjustment_grid values('"+TextBox1.Text+"','" + itemno1.SelectedItem.Text + "','" + adj1.SelectedItem.Text + "','" + store1.SelectedItem.Text + "','" + qty1.Text + "','" + cost1.Text + "')", conn, tran);
        cmd1.ExecuteNonQuery();
    }
    //adjustment grid row 2
    if (itemno2.SelectedItem.Text != "please select")
    {
        SqlCommand cmd2 = new SqlCommand("INSERT INTO adjustment_grid values('" + TextBox1.Text + "','" + itemno2.SelectedItem.Text + "','" + adj2.SelectedItem.Text + "','" + store2.SelectedItem.Text + "','" + qty2.Text + "','" + cost2.Text + "')", conn, tran);
        cmd2.ExecuteNonQuery();
    }
    //adjustment grid row 3
    if (itemno3.SelectedItem.Text != "please select")
    {
        SqlCommand cmd3 = new SqlCommand("INSERT INTO adjustment_grid values('" + TextBox1.Text + "','" + itemno3.SelectedItem.Text + "','" + adj3.SelectedItem.Text + "','" + store3.SelectedItem.Text + "','" + qty3.Text + "','" + cost3.Text + "')", conn, tran);
        cmd3.ExecuteNonQuery();
    }
    tran.Commit();
}

您还应该使用参数而不是字符串连接,但这是另一个问题…

我也不会直接引用你的控件。将这种类型的逻辑放在函数的单独类中,该函数具有用于各种选项的参数。这样你就可以将它与UI解耦,并在以后必要时重用它。