向 .NET 程序集中的 ADODB 命令添加参数时出错

发布于 2024-10-14 14:18:23 字数 1324 浏览 3 评论 0原文

我有一个由经典 ASP 页面使用的 .NET 程序集。我创建了一个返回 ADODB 记录集的方法。在我的 ADODB 命令对象中,我使用以下格式向 adCmdStoredProc CommandType 属性提供参数...

With ADODBCmd 
.ActiveConnection = ADODBConn
.Prepared = True
.CommandType = CommandTypeEnum.adCmdStoredProc
.NamedParameters = True
.CommandText = Sql_GetMyBook   
.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))
End With

我收到转换错误...

System.Exception 未处理
消息=System.InvalidCastException: 无法转换类型的 COM 对象 “System.__ComObject”到类类型 'ADODB.InternalParameter'。实例 代表 COM 组件的类型 不能转换为不支持的类型 代表 COM 组件;然而他们 可以强制转换为接口,只要 底层COM组件支持 QueryInterface 调用 IID 界面。

在线:

.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))

有什么想法吗?

存储过程:

ALTER PROCEDURE [dbo].[GetMybook]
    -- Add the parameters for the stored procedure here
    @book char(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT BookTitle, Author, PulishedDate
    FROM        Library
    WHERE       BookTitle=@book

I have a .NET assembly that is being consumed by a classic ASP page. I've created a method that returns a ADODB recordset. In my ADODB command object I'm supplying parameters using the following format to a adCmdStoredProc CommandType property...

With ADODBCmd 
.ActiveConnection = ADODBConn
.Prepared = True
.CommandType = CommandTypeEnum.adCmdStoredProc
.NamedParameters = True
.CommandText = Sql_GetMyBook   
.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))
End With

I get a casting error ...

System.Exception was unhandled
Message=System.InvalidCastException:
Unable to cast COM object of type
'System.__ComObject' to class type
'ADODB.InternalParameter'. Instances
of types that represent COM components
cannot be cast to types that do not
represent COM components; however they
can be cast to interfaces as long as
the underlying COM component supports
QueryInterface calls for the IID of
the interface.

at line:

.Parameters.Append(.CreateParameter("@book", DataTypeEnum.adChar, ParameterDirectionEnum.adParamInput, 50, MyBook))

Any ideas?

Stored Proc:

ALTER PROCEDURE [dbo].[GetMybook]
    -- Add the parameters for the stored procedure here
    @book char(50)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    SELECT BookTitle, Author, PulishedDate
    FROM        Library
    WHERE       BookTitle=@book

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

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

发布评论

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

评论(2

漫雪独思 2024-10-21 14:18:24

Microsoft ActiveX 数据对象库 2.7 中“.CreateParameter”方法的返回值之间存在差异(无论是否有意)

- 返回“ADODB.InternalParameter”(ADODB.Command 对象所期望的)

2.8 - 返回“System” .__ComObject”(ADODB.Command 无法处理或不知道如何处理)

出于我的目的,我必须将引用从 2.8 更改为 2.7 库,以便将创建的参数附加到命令对象。

感谢 Chris Behrens 帮助我缩小了解决方案的搜索范围。

There is a difference (unintended or not) between the return value of the ".CreateParameter" method in Microsoft ActiveX Data objects Library

2.7 - Returns "ADODB.InternalParameter" (which is expected by the ADODB.Command object)

2.8 - Returns "System.__ComObject" (which the ADODB.Command can't handle or doesn't know what to do with)

For my purposes I had to change my reference from 2.8 to 2.7 library in order to append parameters created to the command object.

Thanks to Chris Behrens for helping me narrow down my search for a solution.

许一世地老天荒 2024-10-21 14:18:24

我认为这与“MyBook”的价值有关。从数据类型来看,我们应该期望它是单个字符,但错误消息似乎表明它是一个成熟的 COM 对象。也许它应该类似于“MyBook.Id”?

I think it has to do with the value of "MyBook". We should expect it to be a single character, from the data type, but the error message seems to be indicating that it's a full-blown COM object. Maybe it should be something like "MyBook.Id"?

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