Re: Accessing text / csv files using ADO in C++



Hi Ralph,

Actually I wasn't using an uninitialised object, because soon after the
declaration of spConn you'll find code to CreateInstance that creates an
object. However, I did try what you suggested and this time I got a much more
informative error "CoInitialize has not been called" instead of "Invalid
pointer" !! I had forgotten to add a call to CoInitialize, so I added it, and
then I crossed that stage. Then I got a few other errors because of other
problems (such as using a blank user name instead of "Admin"), and when I
solved them, things started working.

So going back, it turns out that the behaviour of:
(1)
_ConnectionPtr spConn;
spConn.CreateInstance(__uuidof(Connection));

is significantly different from that of:
(2)
_ConnectionPtr spConn(__uuidof(Connection));

though internally the constructor called in (2) calls CreateInstance used in
(1). This difference is because CreateInstance returns hr, so we're expected
to check it. But the ctor in (2) calls _com_issue_error which internally does
a throw.

Thanks, Ralph.

Venkatesh

"Ralph" wrote:


"Venkatesh" <Venkatesh@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BDA5F613-C960-454F-AD1B-D95B66AC9422@xxxxxxxxxxxxxxxx
ADO may not be the best way of accessing data when developing in C++, yet
can
somebody tell me how to access a csv file (comma-separated values) using
ADO
in C++. The following code gives me an error "Invalid pointer" when I call
the Open method of the ADO Connection object.

_ConnectionPtr spConn;
spConn.CreateInstance(__uuidof(Connection));
spConn->Open(L"Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq=C:\\;Extensions=csv,txt;", _bstr_t(L""), _bstr_t(L""),
adConnectUnspecified );

I get the same error even when Open() is called like this:

spConn->Open(L"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\;Extended
Properties=\"text;HDR=YES;FMT=Delimited(,)\"", _bstr_t(L""), _bstr_t(L""),
adConnectUnspecified );

I actually got these two variants for Open() from some VB samples, so I
don't know if I can use that way here. Of course, in VB, the last three
parameters to Open() are optional, so I passed blank values etc here.

try
_ConnectionPtr spConn(__uuidof(Connection));

You are trying to call a method on an uninitialized object.
After you have a Connection object you can use the Errors Collection to
deliminate additional problems.

-ralph
.