[英]Error when Passing a DataTable from vb.net to a stored procedure in SQL Server
I have a stored procedure that receives a table, but whenever I make the insert I get this error: 我有一个接收表的存储过程,但是每当进行插入操作时,都会出现此错误:
Cannot convert a char value to money.
无法将char值转换为金钱。 The char value has incorrect syntax.
char值的语法不正确。
I create a table as type 我创建一个表作为类型
CREATE TYPE dbo.Example AS TABLE
(
Id_Exmp int,
Discount_Amount Money
)
I have a stored procedure that takes a datatable as a parameter: 我有一个将数据表作为参数的存储过程:
CREATE PROCEDURE dbo.SP_Exmp
@datatable dbo.Example READONLY
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO dbo.destination_table(column_list)
SELECT column_list
FROM @datatable;
END
This is the code I have in VB.net: 这是我在VB.net中拥有的代码:
Function Example(ByVal dtTable as datatable)
Try
ConexionSQL = New SqlConnection(IniciaConexion)
Command = New SqlCommand("SP_Exmp", ConexionSQL)
Command.CommandType = CommandType.StoredProcedure
'Parametros a Utilizar
Command.Parameters.AddWithValue("@datatable ", dtTable )
ConexionSQL.Open()
If Command.ExecuteNonQuery() Then
Return True
Else
Return False
End If
ConexionSQL.Close()
ConexionSQL.Dispose()
Command.Dispose()
Catch ex As Exception
MensajeError(ex, "Error")
Return False
End Try
End Function
A couple of things: 有两件事:
Command.Parameters.AddWithValue("@datatable ", dtTable )
You need to set its SqlDbType: 您需要设置其SqlDbType:
Command.Parameters.AddWithValue("@datatable ", dtTable).SqlDbType = SqlDbType.Structured;
Using ConexionSQL = New SqlConnection(IniciaConexion) Using command As New SqlCommand End Using End Using
DataTable dtTable = new DataTable(); dtTable.Columns.Add("Id_Exmp", typeof(int)); dtTable.Columns.Add("Discount_Amount", typeof(decimal));
I found out what it was the problem, the main issue was the order how I create the dtTable, in order to fill correctly the table type, I most create the column with the same order on the type, as an Example. 我发现了问题所在,主要的问题是创建dtTable的顺序,为了正确填充表类型,我以示例的方式在该类型上以相同的顺序创建了列。
Dim DtTable as datatable = Exec_Query(Query:="Select Id_exm, Discoun..", LRw:=false) Dim DtTable作为datatable = Exec_Query(Query:=“ Select Id_exm,Discoun ..”,LRw:= false)
for wherever reason I put in disorder, discount first and id second, I didn't pay attention to that, 无论出于什么原因,我无序地打折,然后是折扣,然后才是ID,我没有注意
then everything works just fine. 然后一切正常。
您必须打开连接。
ConexionSQL.Open().
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.