简体   繁体   English

将数据表从vb.net传递到SQL Server中的存储过程时出错

[英]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: 有两件事:

  1. Command.Parameters.AddWithValue("@datatable ", dtTable )

You need to set its SqlDbType: 您需要设置其SqlDbType:

Command.Parameters.AddWithValue("@datatable ", dtTable).SqlDbType = SqlDbType.Structured;


2. Dispose your command before the connection, or even better use Using pattern: 2.在连接之前放置您的命令,甚至更好地使用“使用模式”:

 Using ConexionSQL = New SqlConnection(IniciaConexion) Using command As New SqlCommand End Using End Using 


3. Instead of the Money DataType, try changing your SQL DataTable to have a second column of Decimal and your .Net Datatable to have a Decimal, eg: 3.尝试更改SQL DataTable使其具有Decimal的第二列,并更改.Net Datatable使其具有Decimal,而不是Money DataType,例如:

 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM