使用2种不同方法的交易

时间:2016-07-21 07:20:31

标签: c# sql-server transactions

我在按钮1按下的事件中在数据库中插入一行,我希望它仅在用户按下按钮1后按钮2时才会提交,否则它应该回滚。我正在使用sql server 2014和visual studio 2015.

private void button5_Click(object sender, EventArgs e)
    {

        tableLayoutPanel1.RowCount = tableLayoutPanel1.RowCount + 1;
        tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.AutoSize, 20F));
        tableLayoutPanel1.Controls.Add(new Label() { Text = sno.ToString() }, 0, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = textBox3.Text }, 1, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = comboBox2.Text }, 2, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = comboBox3.Text }, 3, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = textBox4.Text }, 4, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = textBox5.Text }, 5, tableLayoutPanel1.RowCount - 1);
        tableLayoutPanel1.Controls.Add(new Label() { Text = textBox6.Text }, 6, tableLayoutPanel1.RowCount - 1);
        tamount = tamount + (Convert.ToInt32(textBox6.Text));
        textBox8.Text = tamount.ToString();
        sno = sno + 1;
        string connetionString = null;
        connetionString = "Data Source=.;Initial Catalog=accounting;Integrated Security=true";
        SqlConnection cnn = new SqlConnection(connetionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO challan_print_sub (challan_no,job_name,paper_stock_name,size,quantity,rate,amount,sno) VALUES(@challan_no,@job_name,@paper_stock_name,@size,@quantity,@rate,@amount,@sno)");
            cmd.Connection = cnn;
            SqlTransaction transac;
            transac = cnn.BeginTransaction("SampleTransaction");
            cmd.Transaction = transac;
            cmd.Parameters.Add("@challan_no", Convert.ToInt32(textBox2.Text));
            cmd.Parameters.Add("@job_name", textBox3.Text);
            cmd.Parameters.Add("@paper_stock_name", comboBox2.Text);
            cmd.Parameters.Add("@size", comboBox3.Text);
            cmd.Parameters.Add("@quantity", Convert.ToInt32(textBox4.Text));
            cmd.Parameters.Add("@rate", Convert.ToInt32(textBox5.Text));
            cmd.Parameters.Add("@amount", Convert.ToInt32(textBox6.Text));
            cmd.Parameters.Add("@sno", sno - 1);
            cmd.ExecuteNonQuery();
            MessageBox.Show("done");
            string Sql3 = "select sum(balance) from accounting.dbo.challan_print_main where party_name='"+comboBox1.Text+"'";
            SqlCommand cmd3 = new SqlCommand(Sql3,cnn);
            Int32.TryParse(cmd3.ExecuteScalar().ToString(), out sum);
            textBox11.Text = sum.ToString();
            cnn.Close();
        }  private void button2_Click(object sender, EventArgs e)
    {
        string connetionString = null;
        Int32.TryParse(textBox8.Text, out a);
        Int32.TryParse(textBox7.Text, out b);
        balance = a - b;
        Trans.commit();

1 个答案:

答案 0 :(得分:0)

我不会自己动手,引用我对这个问题的评论:

  

如果用户等了10分钟,这是一个非常非常坏主意   在按下第二个按钮之前。你想保留你的   交易锁定了那个时间?它会超时。

但如果你坚持:

您可以将连接对象作为类字段共享(对于事务对象也是如此)。在第一个按钮单击时创建new。 当用户单击第二个按钮时使用相同的连接和事务对象,并在第二个按钮的情况下提交事务。

SqlConnection cnn;
SqlTransaction transac;

private void button1_Click(object sender, EventArgs e)
{
    cnn = new SqlConnection();
    transac = new SqlTransaction();
    // continue work
}

private void button2_Click(object sender, EventArgs e)
{

      // use cnn and transac directly, if null return from this method

      Trans.commit();
}