为什么我需要 Odbc 读取器实例,但现在需要 Odbc 连接?

发布于 2024-08-10 03:38:56 字数 280 浏览 1 评论 0原文

在类级别,我声明:

System::Data::Odbc::OdbcConnection conn;
System::Data::Odbc::OdbcDataReader datareader; //doesnt work
System::Data::Odbc::OdbcDataReader^  datareader; //works

但是,dataReader 必须声明为 OdbcDataReader^。我不明白为什么。

On the class level, I have declared:

System::Data::Odbc::OdbcConnection conn;
System::Data::Odbc::OdbcDataReader datareader; //doesnt work
System::Data::Odbc::OdbcDataReader^  datareader; //works

However, the dataReader has to be declared as OdbcDataReader^. I dont understand why.

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

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

发布评论

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

评论(2

久随 2024-08-17 03:38:56

使用OdbcConnection conn;,您可以直接(在堆栈上)实例化连接对象。不确定这是否是一个好主意,但这是可能的。

您不能使用 OdbcDataReader datareader; 来执行此操作,因为该类仅具有隐藏(私有/内部)构造函数。根据设计,您不应该直接创建 DataReader,而是通过调用 ExecuteReader() 来获取它们。并且 ExecuteReader 返回 OdbcDataReader^ 。请参阅 MSDN

With OdbcConnection conn; you are instantiating a connection object directly (on the stack). Not sure if that is a good idea but it is possible.

You cannot do that with a OdbcDataReader datareader; because that class only has hidden (private/internal) constructors. That is by design, you are not supposed to create DataReaders directly but instead get them from a call to ExecuteReader(). And ExecuteReader returns OdbcDataReader^ . See MSDN.

┼── 2024-08-17 03:38:56

我不确定这是否回答了您的问题,但这里是:

Connection 和 DataReader 类在 .NET Framework 上成对出现,具体取决于所使用的数据库技术。所以你有OdbcConnectionOdbcDataReader,还有SqlConnectionSqlDataReader等。你总是成对使用它们。无论如何请注意,所有这些都实现了通用接口 IDataConnectionIDataReader

编辑。好吧,我完全误解了这个问题。 :-/

我通常使用C#,而不是C++,但我认为这是因为你可以直接创建一个新的OdbcConnection实例,但是对于OdbcDataReader,你需要通过对相应的OdbcCommand执行ExecuteReader方法来获取实例。 ExecuteReader 返回指向新 OdbcDataReader 对象的指针。

I'm not sure if this answers your question but here goes:

Connection and DataReader classes come in pairs on the .NET Framework, depending on the database technology used. So you have OdbcConnection and OdbcDataReader, also SqlConnection and SqlDataReader, etc. You have always to use them in pairs. Note anyway that all of these implement the common interfaces IDataConnection and IDataReader.

EDIT. Fine, I completely misunderstood the question. :-/

I usually use C#, not C++, but I think that it is because you can directly create a new instance of OdbcConnection, but as for the OdbcDataReader, you need to obtain an instance by executing the ExecuteReader method on the corresponding OdbcCommand. ExecuteReader returns a pointer to a new OdbcDataReader object.

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