在我的窗口应用程序中,我需要执行一系列 sql 命令。如果所有 sqlcommands 都成功执行,则没有问题,但是当第 2 或第 3 个命令出错时,这对我来说是个大问题,因为第 1 条命令已执行,但第 2 和第 3 条命令没有执行。在这里,我想要执行所有命令或不执行任何命令。我的代码如下:
SqlCommand cmd = new SqlCommand("CREATE TABLE [dbo].[" + textBox8.Text + "_stock]("
+ "[date] [date] NOT NULL PRIMARY KEY CLUSTERED,"
+ "[openingstock] [int] NOT NULL,"
+ "[receipt] [int] NOT NULL,"
+ "[totalstock] [int] NOT NULL,"
+ "[sell] [int] NOT NULL,"
+ "[closingstock] [int] NOT NULL,"
+ ") ON [PRIMARY]", connectionsql);
cmd.ExecuteNonQuery();
cmd.Dispose();
SqlCommand cmd1 = new SqlCommand("insert into " + textBox8.Text + "_stock values(@date,0,0,0,0,0)", connectionsql);
cmd1.Parameters.AddWithValue("date", dateTimePicker3.Value);
cmd1.ExecuteNonQuery();
cmd1.Dispose();
cmd1.Parameters.Clear();
SqlCommand cmd2 = new SqlCommand("insert into rate values ('" + textBox12.Text + "','" + textBox8.Text + "_stock','" + double.Parse(textBox7.Text) + "','" + comboBox4.SelectedItem + "')", connectionsql);
int z = cmd2.ExecuteNonQuery();
cmd2.Dispose();