Ich füge eine Zeile in der Datenbank auf ein Ereignis einer Taste1 drücken und ich möchte es nur Committed, wenn Benutzer Taste 2 nach Taste 1 drücken, sonst sollte es zurückrollen. Ich bin mit SQL Server 2014 und Visual Studio 2015.mit Transaktionen in 2 verschiedenen Methoden
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();
Das ist eine ** sehr, sehr ** schlechte Idee, was ist, wenn der Benutzer 10 Minuten gewartet hat, bevor er die zweite Taste gedrückt hat. Möchten Sie Ihre Transaktion für diese Zeit gesperrt halten? Es würde eine Auszeit geben. – user3185569
nein, er wird es nicht eine Buchhaltungssoftware, so wird es nicht so viel brauchen –