Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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# 变量名'@产品标识';已经申报了。变量名在查询批处理或存储过程中必须是唯一的_C#_Sql Server_Winforms_Datagridview - Fatal编程技术网

C# 变量名'@产品标识';已经申报了。变量名在查询批处理或存储过程中必须是唯一的

C# 变量名'@产品标识';已经申报了。变量名在查询批处理或存储过程中必须是唯一的,c#,sql-server,winforms,datagridview,C#,Sql Server,Winforms,Datagridview,我将数据从datagridview插入数据库表。但我在标题中写的例外情况中出现的错误。值也保存在数据库表中 private void btnSavePurchases_Click(object sender, EventArgs e) { if ((string.IsNullOrEmpty(txtPGrandTotal.Text)) || (txtPGrandTotal.Text == "0")) { MessageBox.Show("No record ava

我将数据从datagridview插入数据库表。但我在标题中写的例外情况中出现的错误。值也保存在数据库表中

private void btnSavePurchases_Click(object sender, EventArgs e)
{
    if ((string.IsNullOrEmpty(txtPGrandTotal.Text)) || (txtPGrandTotal.Text == "0")) 
    {
        MessageBox.Show("No record available");
    }
    else
    { 
        try
        {           
            string query1 = "INSERT INTO purchases (productId) values (@product_Id)";
            command = DBConnectivity.getCommandForQuery(query1, connection);
            for (int i = 0; i < dGvPurchases.Rows.Count; i++)
            {
                //   command.Parameters.Clear();
                command.Parameters.AddWithValue("product_Id", dGvPurchases.Rows[i].Cells[9].Value);
                command.ExecuteNonQuery();
            }            
        }
        catch (Exception ex)
        {
        }
    }
}
private void btnsavephases\u单击(对象发送者,事件参数e)
{
if((string.IsNullOrEmpty(txtPGrandTotal.Text))| |(txtPGrandTotal.Text==“0”))
{
MessageBox.Show(“无可用记录”);
}
其他的
{ 
尝试
{           
字符串query1=“插入采购(productId)值(@product_Id)”;
command=DBConnectivity.getCommandForQuery(query1,连接);
对于(int i=0;i
我在这里的范围内没有看到
命令。可能是一个类变量。这是你问题的一部分

命令
变量的构造移动到方法内部:

var command = DBConnectivity.getCommandForQuery(...);
^^^
其次,您的外观会导致问题。不能反复添加相同的参数。如果要插入多行,请添加多条
insert
语句。在
循环的
内创建
命令。

AddWithValue("product_Id"...
应该是

AddWithValue("@product_Id"...
顺便说一下,由于您在for循环中添加了参数,因此需要在for循环的顶部清除这些参数。这就是为什么需要取消注释
命令.Parameters.Clear()
部分。如果不使用
Clear
方法,您会一次又一次地尝试将相同的参数名称添加到同一个命令中,并且在第二次迭代中会出现错误

作为另一种解决方案,您可以在循环外部声明参数名称,并在循环内部添加它的值

尽量不要使用
AddWithValue
。使用
.Add()
重载指定数据库类型及其大小


不要忘了使用它来处理您的命令对象。

问题是
command.Parameters.AddWithValue
将参数添加到参数列表中。不能添加两次参数。由于多次调用该命令,请在循环外部定义参数并在循环内部设置值:

string query1 = "INSERT INTO purchases (productId) values (@product_Id)";
command = DBConnectivity.getCommandForQuery(query1, connection);

// Define the Parameter just once
var param = command.Parameters.Add("@product_Id", SqlDbType.Int);

for (int i = 0; i < dGvPurchases.Rows.Count; i++)
{  // set value while looping
   param.Value = dGvPurchases.Rows[i].Cells[9].Value;
   command.ExecuteNonQuery();
}     
string query1=“插入采购(productId)值(@product_Id)”;
command=DBConnectivity.getCommandForQuery(query1,连接);
//只定义一次参数
var param=command.Parameters.Add(“@product_Id”,SqlDbType.Int);
对于(int i=0;i
AddWithValue(“产品Id”)和AddWithValue(“产品Id”)将起作用。正如您所指出的,问题在于for循环。@ShellShock确实存在,但作为最佳实践,最好是精确匹配,最好使用
@
将它们指定为IMO的参数。我将把它留在这里。您将继续为
for的每次迭代添加
product_id
参数de>loop。不要这样做!定义一次参数,在循环外部,在循环内部只设置值…..参数化查询“(@product\U Id int)插入到采购(productId)值中(@prod”需要参数“@product\U Id”,但未提供该参数。