VB.NET过程或函数指定了太多参数

时间:2016-08-04 16:31:55

标签: sql vb.net

我有以下存储过程:

CREATE PROCEDURE MyProc    
@posted_xml_body xml    
AS    
INSERT INTO MyTable    
(post_datetime, post_body)    
VALUES    
(getdate(), @posted_xml_body)

以下VB代码:

Using aConnection As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings(connectionString).ConnectionString)
    aConnection.Open()
    Dim aCommand As New Data.SqlClient.SqlCommand("MyProc", aConnection)    
    aCommand.CommandType = Data.CommandType.StoredProcedure
    aCommand.Parameters.AddWithValue("@posted_xml_body", aXMLString)

    Dim rows_affected As Integer = aCommand.ExecuteNonQuery()

    aCommand.Dispose()
    aConnection.Close()

    Return rows_affected

End Using

但是,我一直收到以下错误

  

“过程或函数指定了太多参数。”

感谢您的任何建议。

2 个答案:

答案 0 :(得分:0)

您粘贴错误或错过了")"这里

VALUES    
(getdate(), @posted_xml_body)

答案 1 :(得分:0)

我会建议几件事。

将列类型更改为nvarchar(max),选择范围标识,只需确保将您的表作为主键,因为vb代码会在尝试将日期转换为整数时给出异常。

CREATE PROCEDURE MyProc    
@posted_xml_body as nvarchar(max)    
AS   
Begin 
INSERT INTO MyTable    
(post_datetime, post_body)    
VALUES    
(getdate(), @posted_xml_body);SELECT SCOPE_IDENTITY()
END

您的Vb代码

Using aConnection As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings(connectionString).ConnectionString)

    Dim rows_affected As Integer = 0

    Try
        aConnection.Open()

        Dim aCommand As New Data.SqlClient.SqlCommand("MyProc", aConnection)

        aCommand.CommandType = Data.CommandType.StoredProcedure
        aCommand.Parameters.AddWithValue("@posted_xml_body", aXMLString)
        rows_affected = aCommand.ExecuteScalar()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
        If aConnection.State = ConnectionState.Open Then
            aConnection.Close()
        End If
    End Try

    Return rows_affected

End Using