【问题标题】:using transactions in 2 different methods以 2 种不同的方法使用事务
【发布时间】:2016-07-21 07:20:30
【问题描述】:

我在按下按钮 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();

【问题讨论】:

  • 这是一个非常非常的坏主意,如果用户在按下第二个按钮之前等待 10 分钟会怎样。您想在这段时间内锁定您的交易吗?它会超时。
  • 不,他不会使用会计软件,所以不会花那么多时间

标签: c# sql-server transactions


【解决方案1】:

我不会自己做下面的事情,引用我对这个问题的评论:

这是一个非常非常的坏主意,如果用户等待 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();
}

【讨论】:

  • 超时需要多少时间
  • @HimanshuGoel AFAIK,Windows 的默认交易时间为 10 分钟。
猜你喜欢
  • 1970-01-01
  • 2019-06-16
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-29
  • 2019-09-23
相关资源
最近更新 更多