以下这样操作,在昨天在内部函数里面可以执行,今天又不可以执行
Dim cmd As new SQLCommand
cmd.Connectio nName = "hh"
For Each dr As System.Data.DataRow In dt.Rows
cmd.CommandText = "Insert Into pt(" & lb & ") Values(" & yw & ")"
For Each ds As System.Data.DataColumn In dt.Columns
Select Case ds.ColumnName
Case "LDATE"
cmd.Parameters.Add("@" & ds.ColumnName ,dr(ds.ColumnName))
Case Else
cmd.Parameters.Add("@" & ds.ColumnName ,dr(ds.ColumnName).tostring)
End Select
Next
cmd.ExecuteNonQuery
Next
---------------------------
版本:2021.5.29.1
---------------------------
代码执行出错,错误信息:
System.ArgumentException: 已添加了具有相同键的项。
在 System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
在 UserCode.Test()
结果出现如上的错误,但是cmd放在里面,如下:
For Each dr As System.Data.DataRow In dt.Rows
Dim cmd As new SQLCommand
cmd.Connectio nName = "hh"
cmd.CommandText = "Insert Into pt(" & lb & ") Values(" & yw & ")"
For Each ds As System.Data.DataColumn In dt.Columns
Select Case ds.ColumnName
Case "LDATE"
cmd.Parameters.Add("@" & ds.ColumnName ,dr(ds.ColumnName))
Case Else
cmd.Parameters.Add("@" & ds.ColumnName ,dr(ds.ColumnName).tostring)
End Select
Next
cmd.ExecuteNonQuery
Next
这样就不报错了,这是什么情况?cmd.CommandText = “值”后,有时清空了之前的设置参数,有时又没有清空?
[此贴子已经被作者于2021/7/23 14:17:14编辑过]