无法将 OdbcConnection 设置为 OdbcCommand.Connection
在类级别,我创建了引用:
System::Data::Odbc::OdbcConnection Connection;
在某种方法中,我想将其设置为 odbcCommand.Connection,如下所示:
::System::Data::Odbc::OdbcCommand Command; Command.Connection=this->连接;
它报告“无法将参数 1 从 'System::Data::Odbc::OdbcConnection' 转换为 'System::Data::Common::DbConnection ^'”
我不明白为什么它谈到 common::DbConnection 如果命令。连接需要 OdBcConnection? 谢谢
On the class level, I have created reference:
System::Data::Odbc::OdbcConnection Connection;
in some method I want to set it to odbcCommand.Connection like this:
::System::Data::Odbc::OdbcCommand Command;
Command.Connection=this->Connection;
It reports "cannot convert parameter 1 from 'System::Data::Odbc::OdbcConnection' to 'System::Data::Common::DbConnection ^'"
I dont understand why it speaks about common::DbConnection if the Command.Connection expects OdBcConnection?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Command.Connection 想要 System::Data::Common::DbConnection 的句柄 (^)
相反,请尝试执行此操作:
一元 % 运算符 (跟踪引用) 返回对 CLI 对象的 CLI 跟踪引用。跟踪引用对句柄和值类型有效。
对评论的回应:
当您以这种方式创建托管对象时:
它是一种特殊类型的 C++/CLI 对象,在堆栈上分配并指向托管堆上的托管对象。它不是有效的托管引用对象。要创建托管引用,您需要执行以下操作。
并且应在构造函数中删除DB连接以保证及时清理。但管理对象不需要,它们会自动删除
Command.Connection wants a handle (^) to a System::Data::Common::DbConnection
Instead try to do this:
The unary % operator (Tracking reference) returns a CLI tracking references to a CLI object. Tracking references are valid to handles and value types.
Response to comment:
When you create a managed object this way:
It's a special type of C++/CLI object that are allocated on the stack and that points to the managed object on the managed heap. It isn't a valid managed reference object. To create a managed reference you need to do the following.
And DB connection should be deleted in the constructor to guarantee timely clean up. But it is not needed for managed object, they are delete automatically