使用命令对象时出错 [英] Error in using command object
本文介绍了使用命令对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友,
我正在使用命令对象执行插入.
我将同一对象用于两个不同的目的
Hello friends,
I was using command object to perform insert.
I''m using same object for two different purpose
1. First it executes StoredProcedure<br />
2. Second it executes simple text<br />
这一切都是在循环中发生的,第一次运行良好,但在第二次运行时却报错:
Too many parameters
我会粘贴您的代码
This all happens in loop, first time it works fine but in second turn it gives error:
Too many parameters
I paste down code for your refrence
Public Function saveDetails(ByVal Clientid As Integer, ByVal Conditions As String) As Integer
Dim strArr As String()
Dim intRecordid As Integer
Dim strQuery As String
Dim flag As Boolean = False
Dim objConn As SqlConnection = getConnection()
Dim objCMD As New SqlCommand("usp_saveTNC_save", objConn)
Dim sp As SqlParameter
strArr = Conditions.Split(",")
Dim strArrVal As String = ""
Try
objCMD.Connection = objConn
If objConn.State = ConnectionState.Closed Then
objConn.Open()
End If
For i As Integer = 0 To strArr.Length - 1
''SAVE RECORD IN tbl_TConditions .:[BEGIN]:.
objCMD.CommandText = "usp_saveTNC_save"
objCMD.CommandType = CommandType.StoredProcedure
strArrVal = strArr(i)
objCMD.Parameters.AddWithValue("@condition", strArrVal)
If flag = False Then
sp = New SqlParameter("@id", SqlDbType.Int)
sp.Direction = ParameterDirection.Output
objCMD.Parameters.Add(sp)
flag = True
End If
objCMD.ExecuteNonQuery()
intRecordid = sp.Value.ToString()
''SAVE RECORD IN tbl_TConditions .:[END]:.
''SAVE RECORD IN tbl_TConditions_client .:[BEGIN]:.
strQuery = "INSERT INTO tbl_TConditions_client VALUES(" & intRecordid & "," & Clientid & ")"
objCMD.CommandText = strQuery.ToString
objCMD.CommandType = CommandType.Text
objCMD.ExecuteNonQuery()
''SAVE RECORD IN tbl_TConditions_client .:[END]:.
Next
Return 1
Catch ex As Exception
Return -1
Finally
objConn.Dispose()
objConn.Close()
objCMD.Dispose()
objCMD = Nothing
End Try
End Function
您知道如何解决此问题吗?
在此先感谢
Any idea, how to resolve this?
Thanks in advance
推荐答案
这是因为您没有为每次迭代清除参数.尝试在Next
This is because you are not clearing the parameters for each iteration. Try addingobjCMD.Parameters.Clear()
above theNext
上方添加objCMD.Parameters.Clear()
.您可以按照walterhevedeich的解决方案进行操作.或在For循环中声明Dim sp As SqlParameter
.
You can follow walterhevedeich''s solution. Or Declare Dim sp As SqlParameter
inside the For loop.
这篇关于使用命令对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文