问题描述
我想创建一个通用的功能,它可以接受TVP。
我收到错误无法参数值从的SqlParameter到IEnumerable`1转换。
昏暗的参数作为新的SqlParameter(@ parName,GetTVP(lstDates))
param.SqlDbType = SqlDbType.Structured
sqlProvider.ExecuteNonQueryV2(sp_Save,CommandType.StoredProcedure,参数) ---------------------
公共职能的ExecuteNonQuery(BYVAL spname作为字符串,为ByRef的returnValue作为整数,BYVAL的ParamArray的parameterValues()作为对象)作为整数 昏暗的连接作为的SqlConnection =无
昏暗的事务作为的SqlTransaction =无
昏暗的命令的SqlCommand =无
昏暗 - [R为整数= -1 尝试 连接=新的SqlConnection(myconnectionString)
命令=新的SqlCommand(spname,连接)
command.CommandType = CommandType.StoredProcedure
connection.Open()
SqlCommandBuilder.DeriveParameters(命令)
Me.SetParameters(命令的parameterValues)
交易= connection.BeginTransaction()
command.Transaction =交易
R = command.ExecuteNonQuery()
器transaction.commit() 抓住EX为例外 最后 结束Try 返回ř结束功能
---------------------私人小组SetParameters(BYVAL CMD作为的SqlCommand,BYVAL的parameterValues()作为对象)
昏暗我作为整数
对于每一个参数作为的SqlParameter在cmd.Parameters
param.Value =的parameterValues(I)
I + = 1
下一个
结束小组
ADDED
我已经更新了code,走SqlCommandBuilder.DeriveParameters(命令)出来,现在传递参数作为一个的SqlParameter列表。现在一切工作正常:)
但我想知道为什么它的工作时,我还没有定义的参数类型SqlDbType.Structured?是不是必需的?的
昏暗面值作为新的列表(中的SqlParameter)
par.Add(新的SqlParameter(@ parName,GetTVP(lstDates))--------------- 连接=新的SqlConnection(myconnectionString)
命令=新的SqlCommand(spname,连接)
command.CommandType = CommandType.StoredProcedure
connection.Open()
Me.SetParameters(命令的parameterValues)
交易= connection.BeginTransaction()
command.Transaction =交易
R = command.ExecuteNonQuery()
器transaction.commit()
你为什么不签名中传递的SqlParameter的列表,而不是作为参数对象的列表?
您可以这样来做;
公共功能的ExecuteNonQuery(BYVAL spname作为字符串,为ByRef的returnValue为整数,为ByRef参数列表(中的SqlParameter))作为整数
然后就可以循环并添加
对于每一对作为的SqlParameter在参数
command.Parameters.Add(对)
下一个
检查此链接了http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx.理想的情况下,建议您定义的参数类型,但它不是强制性的。
I am trying to create a general purpose function which can accept tvp.
I am getting error 'Failed to convert parameter value from a SqlParameter to a IEnumerable`1.'
Dim param As New SqlParameter("@parName", GetTVP(lstDates))
param.SqlDbType = SqlDbType.Structured
sqlProvider.ExecuteNonQueryV2("sp_Save", CommandType.StoredProcedure, param)
---------------------
Public Function ExecuteNonQuery(ByVal spname As String, ByRef returnValue As Integer, ByVal ParamArray parameterValues() As Object) As Integer
Dim connection As SqlConnection = Nothing
Dim transaction As SqlTransaction = Nothing
Dim command As SqlCommand = Nothing
Dim r As Integer = -1
Try
connection = New SqlConnection(myconnectionString)
command = New SqlCommand(spname, connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
SqlCommandBuilder.DeriveParameters(command)
Me.SetParameters(command, parameterValues)
transaction = connection.BeginTransaction()
command.Transaction = transaction
r = command.ExecuteNonQuery()
transaction.Commit()
Catch ex As Exception
Finally
End Try
Return r
End Function
---------------------
Private Sub SetParameters(ByVal cmd As SqlCommand, ByVal parameterValues() As Object)
Dim i As Integer
For Each param As SqlParameter In cmd.Parameters
param.Value = parameterValues(i)
i += 1
Next
End Sub
ADDED
I have updated the code and take the "SqlCommandBuilder.DeriveParameters(command)" out and now passing parameter as a sqlparameter list. All works fine now :)
But i like to know why is it working when i have not defined the parameter type to SqlDbType.Structured ? Isn't it required?
Dim par As New List(Of SqlParameter)
par.Add(New SqlParameter("@parName", GetTVP(lstDates))
---------------
connection = New SqlConnection(myconnectionString)
command = New SqlCommand(spname, connection)
command.CommandType = CommandType.StoredProcedure
connection.Open()
Me.SetParameters(command, parameterValues)
transaction = connection.BeginTransaction()
command.Transaction = transaction
r = command.ExecuteNonQuery()
transaction.Commit()
Why don't you pass in the signature a list of SqlParameter instead of a list of objects as parameters?
You can do it this way;
Public Function ExecuteNonQuery(ByVal spname As String, ByRef returnValue As Integer, ByRef parameters As List(Of SqlParameter)) As Integer
Then you can loop and add
For Each para As SqlParameter In parameters
command.Parameters.Add(para)
Next
Check this link out http://geekswithblogs.net/Rhames/archive/2008/10/29/why-you-should-always-specify-the-sqldbtype-for-an-ado.net.aspx. Ideally and recommended that you define the parameter type, however it is not mandatory.
这篇关于TVP - 无法参数值从SqlParameter的转换为IEnumerable`1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!