The variable name ‘@’ has already been declared and must declare scalar variable @.

huangapple go评论74阅读模式
英文:

The variable name '@' has already been declared & must declare scalar variable @

问题

我想使用sqlparameters和for循环从DataGridView插入数据到数据库:

Dim sqlAddCmd As New SqlCommand

sqlAddCmd.CommandText =
                        "INSERT INTO TableName (Val,Idx,Ref,Sta,Col,Point,Mat,Refer,Cover,IDNo,Mas)" _
                            & "VALUES (@Val,@No,@Ref,@Sta,@Col,@Point,@Mat,@Refer,@Cover,@IDNo,@Mast)"

sqlAddCmd.Transaction = transaction

With sqlAddCmd.Parameters
    .Add("@Val", SqlDbType.Int)
    .Add("@No", SqlDbType.TinyInt)
    .Add("@Ref", SqlDbType.Char)
    .Add("@Sta", SqlDbType.Char)
    .Add("@Col", SqlDbType.VarChar)
    .Add("@Point", SqlDbType.SmallInt)
    .Add("@Mat", SqlDbType.VarChar)
    .Add("@Refer", SqlDbType.Int)
    .Add("@Cover", SqlDbType.TinyInt)
    .Add("@IDNo", SqlDbType.VarChar)
    .Add("@Mast", SqlDbType.Char)
End With

For intI = 0 To daGridView.Rows.Count - 1
    With sqlAddCmd
        .Parameters("@Val").Value = lngNewID
        .Parameters("@No").Value = daGridView.Rows(intI).Cells("dgvNo").Value
        .Parameters("@Ref").Value = daGridView.Rows(intI).Cells("dgvRef").Value
        .Parameters("@Sta").Value = daGridView.Rows(intI).Cells("dgvSta").Value
        .Parameters("@Col").Value = daGridView.Rows(intI).Cells("dgvCol").Value
        .Parameters("@Point").Value = daGridView.Rows(intI).Cells("dgvPoint").Value
        .Parameters("@Mat").Value = daGridView.Rows(intI).Cells("dgiMat").Value
        .Parameters("@Refer").Value = daGridView.Rows(intI).Cells("dgiRefer").Value
        .Parameters("@Cover").Value = daGridView.Rows(intI).Cells("dgiCover").Value
        .Parameters("@IDNo").Value = daGridView.Rows(intI).Cells("dgiIDNo").Value
        .Parameters("@Mast").Value = daGridView.Rows(intI).Cells("dgiMast").Value
    End With
    sqlAddCmd.ExecuteNonQuery()
Next

但是当我尝试插入时,会显示错误代码:

变量名@Val已经声明。变量必须在查询批处理或存储过程内是唯一的。
必须声明标量变量@No。

我知道"Val"必须是唯一的,所以我根据其他人的建议进行了更改,但仍然遇到这样的问题。是否有遗漏的东西?

英文:

I want to insert data from DataGridView to the database by using sqlparameters and for loop:

Dim sqlAddCmd As New SqlCommand

sqlAddCmd.CommandText =
                        "INSERT INTO TableName (Val,Idx,Ref,Sta,Col,Point,Mat,Refer,Cover,IDNo,Mas)" _
                            & "VALUES (@Val,@No,@Ref,@Sta,@Col,@Point,@Mat,@Refer,@Cover,@IDNo,@Mast)"

            sqlAddCmd.Transaction = transaction

            With sqlAddCmd.Parameters
                .Add("@Val", SqlDbType.Int)
                .Add("@No", SqlDbType.TinyInt)
                .Add("@Ref", SqlDbType.Char)
                .Add("@Sta", SqlDbType.Char)
                .Add("@Col", SqlDbType.VarChar)
                .Add("@Point", SqlDbType.SmallInt)
                .Add("@Mat", SqlDbType.VarChar)
                .Add("@Refer", SqlDbType.Int)
                .Add("@Cover", SqlDbType.TinyInt)
                .Add("@IDNo", SqlDbType.VarChar)
                .Add("@Mast", SqlDbType.Char)

            End With

            For intI = 0 To daGridView.Rows.Count
                With sqlAddCmd
                    .Parameters("@Val").Value = lngNewID
                    .Parameters("@No").Value = daGridView.Rows(intI).Cells("dgvNo").Value
                    .Parameters("@Ref").Value = daGridView.Rows(intI).Cells("dgvRef").Value
                    .Parameters("@Sta").Value = daGridView.Rows(intI).Cells("dgvSta").Value
                    .Parameters("@Col").Value = daGridView.Rows(intI).Cells("dgvCol").Value
                    .Parameters("@Point").Value = daGridView.Rows(intI).Cells("dgvPoint").Value
                    .Parameters("@Mat").Value = daGridView.Rows(intI).Cells("dgiMat").Value
                    .Parameters("@Refer").Value = daGridView.Rows(intI).Cells("dgiRefer").Value
                    .Parameters("@Cover").Value = daGridView.Rows(intI).Cells("dgiCover").Value
                    .Parameters("@IDNo").Value = daGridView.Rows(intI).Cells("dgiIDNo").Value
                    .Parameters("@Mast").Value = daGridView.Rows(intI).Cells("dgiMast").Value
                End With
                sqlAddCmd.ExecuteNonQuery()
            Next

But when I want to insert this it will show the error code

> The Variable Name @Val has already been declared. Variable must be unique within a query batch or stored procedure.
> Must declare the scalar variable @No.

I know the val must be unique so I do that based on what the others suggest but still get the problem like this. Is there something missing?

答案1

得分: 0

我明白了。问题是我已经使用了@Val参数。所以我创建了一个新的参数叫做@ValDG。然后,我不再使用For ... Loop,而是使用Do ... While。所以代码如下:

intI = 0
Do While intI <= datagrid.Rows.Count - 1
    sqlcommand.commandText = "INSERT INTO TableName (Val,Idx,Ref,Sta,Col,Point,Mat,Refer,Cover,IDNo,Mas)" _
                            & "VALUES (@ValDG,@No,@Ref,@Sta,@Col,@Point,@Mat,@Refer,@Cover,@IDNo,@Mast)"
    sqlcommand.Transaction = transaction
    
    With sqlcommand.parameters
        .Add("@ValDG",SqlDbType.int).Value = lngNewID
        ' 其他参数设置...
    End With
    sqlcommand.ExecuteNonQuery()
    intI +=1
Loop

希望这对你有帮助。抱歉回复晚了。

英文:

I got the answer. The problem is I already use the @Val parameter. So I create new Parameter called @ValDG. And then instead of using For ... Loop,
I use Do ... While. So the code is like this:

intI = 0
Do While intI &lt;= datagrid.Rows.Count - 1
sqlcommand.commandText = &quot;INSERT INTO TableName (Val,Idx,Ref,Sta,Col,Point,Mat,Refer,Cover,IDNo,Mas)&quot; _
                        &amp; &quot;VALUES (@ValDG,@No,@Ref,@Sta,@Col,@Point,@Mat,@Refer,@Cover,@IDNo,@Mast)&quot;
sqlcommand.Transaction = transaction

With sqlcommand.parameters
    .Add(&quot;@ValDG&quot;,SqlDbType.int).Value = lngNewID
    .....
End With
sqlcommand.ExecuteNonQuery()
intI +=1
Loop

I hope it will help. Sorry for the late answer post.

huangapple
  • 本文由 发表于 2023年7月24日 16:30:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752668.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定