Re: Convert Char to LPCOLESTR using OLEDB.H

Duncan Smith <[email protected]> Wed, 17 Apr 2002 19:18:49 -0400
Newsgroups gmane.comp.windows.devel.oledb.devel
Message-ID <[email protected]>
Matt,

There are some useful string conversion macros defined in an ATL include
file, atlconv.h. If you are not already using ATL in your project, I think
you just need to #include <atlconv.h> to use the macros. Here is your sample
modified to use the A2COLE macro, which converts an LPCSTR to an LPCOLESTR:

char m_szSQLCommand[6000];
USES_CONVERSION;

strcat(m_szSQLCommand," SELECT COUNT(*) FROM DEVDTA.F5549018  ");

m_pCommandText->SetCommandText(DBGUID_DBSQL,  A2COLE(m_szSQLCommand) );

Another alternative is to use the Win32 API MultiByteToWideChar.

Yet another alternative is to use WCHAR in place of char and wcscat in place
of strcat, etc. Then no string conversion is required.

Duncan Smith


-----Original Message-----
From: Long, Matthew Jr. (Inland) [mailto:[email protected]]
Sent: Wednesday, April 17, 2002 7:00 PM
To: [email protected]
Subject: [OLEDB_DEV] Convert Char to LPCOLESTR using OLEDB.H



I'm trying to use OLEDB.H for the first time, and I got everything working
the way I want except for the T-SQL Command that I build on the fly. I have
a char variable that I append to using strcat. I need to convert this char
buffer to LPCOLESTR, I just don't know how, at least to where the
SetCommandText method will recononize the text in the buffer.

// My char buffer looks like this......

char m_cSQLCommand[6000];
strcat(m_cSQLCommand," SELECT COUNT(*) FROM DEVDTA.F5549018  ");



// The following is the OleDB Interface I'm using to pass my T-SQL buffer
string.
// This compiles with no errors but it doesn't work.

m_pCommandText->SetCommandText(DBGUID_DBSQL, (LPCOLESTR) m_szSQLCommand );



// Note if I do it this way it works! But this isn't going to work for me,
because I have to build this string on the fly, and the statement can be
different each time I loop through.

m_pCommandText->SetCommandText(DBGUID_DBSQL, OLESTR("SELECT COUNT(*) FROM
DEVDTA.F5549018 "));


Any ideas would be appreciated.....

VC 6.0 SP5 Win32

Thanks!

Matt