无法将 OdbcConnection 设置为 OdbcCommand.Connection

发布于 2024-08-09 17:25:07 字数 383 浏览 2 评论 0原文

在类级别,我创建了引用:

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 技术交流群。

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

发布评论

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

评论(1

终难愈 2024-08-16 17:25:07

Command.Connection 想要 System::Data::Common::DbConnection 的句柄 (^)

public:
property OdbcConnection^ Connection {
    OdbcConnection^ get ();
    void set (OdbcConnection^ value);
}

相反,请尝试执行此操作:

System::Data::Odbc::OdbcCommand Command; 
Command.Connection = %Connection;

一元 % 运算符 (跟踪引用) 返回对 CLI 对象的 CLI 跟踪引用。跟踪引用对句柄和值类型有效。

对评论的回应:

当您以这种方式创建托管对象时:

OdbcConnection Connection;

它是一种特殊类型的 C++/CLI 对象,在堆栈​​上分配并指向托管堆上的托管对象。它不是有效的托管引用对象。要创建托管引用,您需要执行以下操作。

OdbcConnection^ Connection; // In the class definition 

// In the class constructor do the following: 
Connection = gcnew OdbcConnection();

并且应在构造函数中删除DB连接以保证及时清理。但管理对象不需要,它们会自动删除

Command.Connection wants a handle (^) to a System::Data::Common::DbConnection

public:
property OdbcConnection^ Connection {
    OdbcConnection^ get ();
    void set (OdbcConnection^ value);
}

Instead try to do this:

System::Data::Odbc::OdbcCommand Command; 
Command.Connection = %Connection;

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:

OdbcConnection Connection;

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.

OdbcConnection^ Connection; // In the class definition 

// In the class constructor do the following: 
Connection = gcnew OdbcConnection();

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

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