I use codes below to inserts values to database by executing stored procedure.
Dim connstring As String = "dsn=test"
Dim oConn As Odbc.OdbcConnection = New Odbc.OdbcConne开发者_JAVA技巧ction(connstring)
Dim com As New Odbc.OdbcCommand("{? = call sp_test1(?,?,?,?)}", oConn)
com.CommandType = CommandType.StoredProcedure
com.Parameters.AddWithValue("@Code", 2)
com.Parameters.AddWithValue("@Name", "2")
com.Parameters.AddWithValue("@Familly", "2")
com.Parameters.AddWithValue("@Pname", "2")
oConn.Open()
com.ExecuteNonQuery()
oConn.Close()
but I got this error
ERROR [HY105] [Microsoft][ODBC SQL Server Driver]Invalid parameter type
and here is my stored procedure
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
Create Procedure [dbo].[sp_test1]
@Code bigint,
@Name nvarchar(50),
@Familly nvarchar(50),
@Pname nvarchar(50),
As
Begin
Insert Into test1
([Code],[Name],[Familly],[Pname])
Values
(@Code,@Name,@Familly,@Pname)
Declare @ReferenceID int
Select @ReferenceID = @@IDENTITY
Return @ReferenceID
End
is there a solution ?
At the first glance, the stored procedure has the 1st parameter of type bigint
whereas it is given a string value in form of an input.
com.Parameters.AddWithValue("@Code", "2")
Change the above to
com.Parameters.AddWithValue("@Code", 2)
Does that work?
EDIT: Can you change this line
Dim com As New Odbc.OdbcCommand("{? = call sp_test1(?,?,?,?)}", oConn)
to
Dim com As New Odbc.OdbcCommand("sp_test1", oConn)
Error occurs because you have not passed the Return Value parameter, which is specified in CommandText, but is missing from parameters collection (the first question mark in the command text IS the return value parameter).
Because parameters must be passed sequentially, ODBC considers, that @Code is the RETURN_VALUE, and @Name is actually @Code, so the type mismatch error occurs.
You must add the following code
com.Parameters.Add("", OdbcType.Int).Direction = ParameterDirection.ReturnValue
com.Parameters.AddWithValue("@Code", 2)
com.Parameters.AddWithValue("@Name", "2")
com.Parameters.AddWithValue("@Familly", "2")
com.Parameters.AddWithValue("@Pname", "2")
精彩评论