C# 我想使用sql和asp.Net将数据插入两个表中

C# 我想使用sql和asp.Net将数据插入两个表中,c#,sql,asp.net,C#,Sql,Asp.net,我想在Voorraad表和配料表中插入数据。在表格Voorraad中,instert后的表格中必须有INGEDINTID、ANTALINVOORRAAD、MinimumVoorraad和CATEGORE 在配料表中必须有新的配料。当我填写文本框并点击“插入”按钮后,我得到错误: System.Data.SqlClient.SqlException:“@MinimumVoorraad”附近的语法不正确 请帮帮我 SqlConnection con = new SqlConnecti

我想在Voorraad表和配料表中插入数据。在表格Voorraad中,instert后的表格中必须有INGEDINTID、ANTALINVOORRAAD、MinimumVoorraad和CATEGORE

在配料表中必须有新的配料。当我填写文本框并点击“插入”按钮后,我得到错误: System.Data.SqlClient.SqlException:“@MinimumVoorraad”附近的语法不正确 请帮帮我

        SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=MYDATASOURCE";
        con.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "Insert into [Voorraad] values(@IngredientID, 
        @AantalInVoorraad, @MinimumVoorraad";
        cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
        cmd.Parameters.AddWithValue("@AantalInVoorraad", txt_aantal.Text);
        cmd.Parameters.AddWithValue("@MinimumVoorraad", txt_minimum.Text);
        cmd.Connection = con;
        cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        cmd.CommandText = "insert into [Ingredient] values(@IngredientID, @IngredientNaam";
        cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
        cmd.Parameters.AddWithValue("@IngredientNaam", txt_ingredient.Text);
        cmd.ExecuteNonQuery();
有人知道向数据库中的多个表插入数据的另一种方法吗??我在整个互联网上搜索了答案,但找不到正确的解决方案。


insert语句缺少值的右括号

为SQlConnection和SQLCommand添加一个using语句,将使其更易于阅读和调试

cmd.CommandText = "insert into [Ingredient] (IngredientID, IngredientNaam) values(@IngredientID, @IngredientNaam)";

insert语句缺少值的右括号

为SQlConnection和SQLCommand添加一个using语句,将使其更易于阅读和调试

cmd.CommandText = "insert into [Ingredient] (IngredientID, IngredientNaam) values(@IngredientID, @IngredientNaam)";

您需要关闭insert语句末尾的括号。注意:使用
语句使用
。请参见您的两个插入都缺少一个右括号。我已对此进行了更改。您需要在insert语句末尾关闭括号。注意:使用
语句使用
。看到你的两个插页都缺少一个右括号了吗?我改了,谢谢你的评论。我尝试了这个,但我得到了错误:“@MinimumVoorraad”附近的语法不正确。我复制了你的insert语句,忘了添加缺少的括号,请尝试我的新编辑。啊,我明白为什么了,括号在双引号之外,我已经编辑过了。谢谢你的评论。我尝试了这个,但我得到了错误:“@MinimumVoorraad”附近的语法不正确。我复制了你的insert语句,忘记添加缺少的括号,请尝试我的新编辑。啊,我明白了为什么,括号在双引号之外,我已经编辑过了。
cmd.CommandText = "insert into [Ingredient] (IngredientID, IngredientNaam) values(@IngredientID, @IngredientNaam)";
        using (SqlConnection con = new SqlConnection(@"Data Source=MYDATASOURCE"))
        {
           con.Open();

           using(SqlCommand cmd = new SqlCommand(
    "Insert into [Voorraad] values(@IngredientID, @AantalInVoorraad, @MinimumVoorraad)", con))
           {
               cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
               cmd.Parameters.AddWithValue("@AantalInVoorraad", txt_aantal.Text);
               cmd.Parameters.AddWithValue("@MinimumVoorraad", txt_minimum.Text);
               cmd.ExecuteNonQuery();
           }

           using(SqlCommand cmd = new SqlCommand(
    "insert into [Ingredient] values(@IngredientID, @IngredientNaam)", con))
           {
              cmd.Parameters.AddWithValue("@IngredientID", txt_ID.Text);
              cmd.Parameters.AddWithValue("@IngredientNaam", txt_ingredient.Text);
              cmd.ExecuteNonQuery();
           }
        }