为什么我需要 Odbc 读取器实例,但现在需要 Odbc 连接?
在类级别,我声明:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
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 toExecuteReader()
. And ExecuteReader returnsOdbcDataReader^
. See MSDN.我不确定这是否回答了您的问题,但这里是:
Connection 和 DataReader 类在 .NET Framework 上成对出现,具体取决于所使用的数据库技术。所以你有
OdbcConnection
和OdbcDataReader
,还有SqlConnection
和SqlDataReader
等。你总是成对使用它们。无论如何请注意,所有这些都实现了通用接口IDataConnection
和IDataReader
。编辑。好吧,我完全误解了这个问题。 :-/
我通常使用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
andOdbcDataReader
, alsoSqlConnection
andSqlDataReader
, etc. You have always to use them in pairs. Note anyway that all of these implement the common interfacesIDataConnection
andIDataReader
.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 theOdbcDataReader
, you need to obtain an instance by executing theExecuteReader
method on the correspondingOdbcCommand
. ExecuteReader returns a pointer to a new OdbcDataReader object.