Why do I need an instance of Odbc reader, but now Odbc connection?
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.
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.
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.
精彩评论