Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 变量名'@Order_id'已声明。变量名在查询批处理或存储过程中必须是唯一的_C# - Fatal编程技术网

C# 变量名'@Order_id'已声明。变量名在查询批处理或存储过程中必须是唯一的

C# 变量名'@Order_id'已声明。变量名在查询批处理或存储过程中必须是唯一的,c#,C#,当我从gridview中插入数据时,我遇到了这个错误,我将数据插入到订单详细信息中,我希望在每个订单项目中保存相同的订单ID,但无法。我正在C窗口中开发。我已经在网上搜索过了,但这并不能解决我的问题。我被困在这一点上。感谢您的帮助 我正在添加我正在执行的代码行 问候 SqlCommand cmd1 = con.CreateCommand(); cmd1.CommandType = CommandType.Text; cmd1.CommandText = "SELECT Top 1 * FR

当我从gridview中插入数据时,我遇到了这个错误,我将数据插入到订单详细信息中,我希望在每个订单项目中保存相同的订单ID,但无法。我正在C窗口中开发。我已经在网上搜索过了,但这并不能解决我的问题。我被困在这一点上。感谢您的帮助

我正在添加我正在执行的代码行

问候

SqlCommand cmd1 = con.CreateCommand();
cmd1.CommandType = CommandType.Text;

cmd1.CommandText = "SELECT Top  1 *  FROM Purchase_Order Order By P_Order_ID desc";
cmd1.ExecuteNonQuery();

DataTable dt = new DataTable();

SqlDataAdapter sda2 = new SqlDataAdapter(cmd1);
sda2.Fill(dt);

int OrderID = 0;

foreach (DataRow dr2 in dt.Rows)
{
    OrderID = Convert.ToInt32(dr2["P_Order_ID"].ToString());

    MessageBox.Show("order id +" +OrderID);
}

SqlCommand com2 = new SqlCommand();
com2.Connection = con;

for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    foreach (DataRow dr2 in dt.Rows)
    {
        OrderID = Convert.ToInt32(dr2["P_Order_ID"].ToString());
    }

    string Query;
    Query = @"INSERT INTO Purchase_Order_Detail (P_Order_ID, ProductID, PSC_ID, Pack_ID, Color, Base_ID, Quantity) VALUES (@OrderID, @ProductID, @Sub_ID, @PackID, @Colors, @BaseID, @Quantity);";

    // com2.Parameters.Add("@OrderID" & dataGridView1.Rows(i).ToString(), dataGridView1(i));

    com2.Parameters.AddWithValue("@OrderID", OrderID);
    com2.Parameters.AddWithValue("@ProductID", dataGridView1.Rows[i].Cells[4].Value);
    com2.Parameters.AddWithValue("@Sub_ID", dataGridView1.Rows[i].Cells[6].Value);
    com2.Parameters.AddWithValue("@PackID", dataGridView1.Rows[i].Cells[8].Value);
    com2.Parameters.AddWithValue("@Colors", dataGridView1.Rows[i].Cells[9].Value);
    com2.Parameters.AddWithValue("@BaseID", dataGridView1.Rows[i].Cells[11].Value);
    com2.Parameters.AddWithValue("@Quantity", dataGridView1.Rows[i].Cells[13].Value);

    com2.CommandText = Query;
    com2.ExecuteNonQuery();

    //com2.Parameters.Clear();
}

您正在尝试重新添加已添加到SqlCommand的参数。在您的情况下,应该在for循环之前添加参数和查询,然后用新值填充这些参数以执行

SqlCommand com2 = new SqlCommand();
com2.Connection = con;
com2.CommandText = @"INSERT INTO Purchase_Order_Detail (P_Order_ID,ProductID,PSC_ID,Pack_ID,Color,Base_ID,Quantity) VALUES (@OrderID,@ProductID,@Sub_ID, @PackID,@Colors,@BaseID,@Quantity);";
com2.Parameters.Add("@OrderID", ...type...);
com2.Parameters.Add("@ProductID", ...type...);
com2.Parameters.Add("@Sub_ID", ...type...);
com2.Parameters.Add("@PackID", ...type...);
com2.Parameters.Add("@Colors", ...type...);
com2.Parameters.Add("@BaseID", ...type...);
com2.Parameters.Add("@Quantity", ...type...);
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
    foreach (DataRow dr2 in dt.Rows)
    {
        OrderID = Convert.ToInt32(dr2["P_Order_ID"].ToString());
    }
    com2.Parameters["@OrderId"].Value = OrderId;
    com2.Parameters["@ProductID"].Value = dataGridView1.Rows[i].Cells[4].Value;  
    ...        

    com2.ExecuteNonQuery();
}

Move SqlCommand com2=新建。。。。在for循环内部。这是因为您试图将orderid添加到同一个命令对象Im second iteration progman,谢谢您的响应。我想知道我必须写什么..键入?我在SQL中设置的参数的数据类型?@OsamaHassan是。Java API在没有任何值的情况下无法知道类型,因此必须为参数指定类型。