使用 odbc 通过 addwithvalue 设置参数到存储过程

发布于 2024-09-24 06:52:13 字数 1070 浏览 0 评论 0原文

我使用下面的代码通过执行存储过程将值插入数据库。

 Dim connstring As String = "dsn=test"
 Dim oConn As Odbc.OdbcConnection = New Odbc.OdbcConnection(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()

但我收到此错误

ERROR [HY105] [Microsoft][ODBC SQL Server Driver]Invalid parameter type

,这是我的存储过程

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

有解决方案吗?

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.OdbcConnection(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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-10-01 06:52:13

乍一看,存储过程的第一个参数为 bigint 类型,而它以输入形式给出了一个字符串值。

com.Parameters.AddWithValue("@Code", "2")

将上面的内容更改为

com.Parameters.AddWithValue("@Code", 2)

这样可以吗?

编辑:您可以将此行

Dim com As New Odbc.OdbcCommand("{? = call sp_test1(?,?,?,?)}", oConn)

更改为

Dim com As New Odbc.OdbcCommand("sp_test1", oConn)

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)

捂风挽笑 2024-10-01 06:52:13

发生错误的原因是您尚未传递在 CommandText 中指定的返回值参数,但参数集合中缺少该参数(命令文本中的第一个问号是返回值参数)。

由于参数必须按顺序传递,ODBC认为@Code是RETURN_VALUE,而@Name实际上是@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")

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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文