From: NightCrawler on
Hi All,
I am a beginner in MFC and Database using ADO.

I am able to connect to MSSQL server properly.
I am also able to connect to a table using _RecordsetPtr-
>Open(TableName,...) and get all of the data from the table, but have

problem when I put some query as the source for the recordset. here is
my code-

/
**********************************************************************************************************************/
CString strQuerry = _T("select * from tblArea"); //tblArea has only
two columns, ID and Area.
HRESULT hr;
_RecordsetPtr rs;
try
{
hr = rs.CreateInstance(__uuidof(Recordset));
ASSERT(SUCCEEDED(hr));

_bstr_t bstrQuerry =
_bstr_t(strQuerry.GetBuffer());

//m_con is successfully connected,
_ConnectionPtr type member variable.
//hr = m_con->Open(GetConnectionString(),
_bstr_t(m_strUserID.GetBuffer()),
//
_bstr_t(m_strPassword.GetBuffer()), adOpenUnspecified);

hr = rs->Open(bstrQuerry,
m_con.GetInterfacePtr(), adOpenStatic,
adLockReadOnly, adCmdTable);
ASSERT(SUCCEEDED(hr));

//----- Some Code to get data from rs -----

rs->Close();
}
catch(_com_error& ce)
{
//AfxMessageBox("Inside Trace");
_bstr_t bstrDescr (ce.Description());
_bstr_t bstrSource (ce.Source());

TRACE (L"------ _com_error exeption thrown
-------\n");
TRACE (L"\tHRESULT = 0x%081x
\n",ce.Error());
TRACE (L"\tHRESULT Description = %s
\n",ce.ErrorMessage());
TRACE (L"\tDescription = %s\n",
(LPCTSTR)bstrDescr);
TRACE (L"\tSource = %s\n",
(LPCTSTR)bstrSource);
}
catch(...)
{
TRACE (L"Unknown Exception was thrown/\n");
}
/
**********************************************************************************************************************/

whenever I try to run this it always throws exception
(DB_E_ERRORSINCOMMAND -> "Incorrect syntax near the keyword 'select'."

I searched on the group and found most of the code written in the same
fashion. I don't understand where I am wrong.

Thanx in advance.
From: Giovanni Dicanio on

"NightCrawler" <muley.rahul(a)gmail.com> ha scritto nel messaggio
news:eaac1a42-2a4f-4ea4-9ddd-04b84f7b67a2(a)v13g2000pro.googlegroups.com...

I'm not sure about your use of CString::GetBuffer() :

> CString strQuerry = _T("select * from tblArea"); //tblArea has only
> two columns, ID and Area.

> _bstr_t bstrQuerry =
> _bstr_t(strQuerry.GetBuffer());

CString has an implicit conversion operator LPCTSTR; however if you like
explicit calls, you may want to use CString::GetString():

_bstr_t bstrQuery( strQuery.GetString() );

Idem for other wrong CString:.GetBuffer() calls following in your code.

Giovanni


From: NightCrawler on
On Oct 16, 1:33 pm, "Giovanni Dicanio"
<giovanniDOTdica...(a)REMOVEMEgmail.com> wrote:
> "NightCrawler" <muley.ra...(a)gmail.com> ha scritto nel messaggionews:eaac1a42-2a4f-4ea4-9ddd-04b84f7b67a2(a)v13g2000pro.googlegroups.com...
>
> I'm not sure about your use of CString::GetBuffer() :
>
> > CString strQuerry = _T("select * from tblArea"); //tblArea has only
> > two columns, ID and Area.
> >                        _bstr_t bstrQuerry =
> > _bstr_t(strQuerry.GetBuffer());
>
> CString has an implicit conversion operator LPCTSTR; however if you like
> explicit calls, you may want to use CString::GetString():
>
>  _bstr_t bstrQuery( strQuery.GetString() );
>
> Idem for other wrong CString:.GetBuffer() calls following in your code.
>
> Giovanni

Hi Giovanni,

There is no problem with CString::GetBuffer() it will just return
the buffer which can easily be typecasted to _bstr_t, I am sure about
it.
I got the answer I was seeking.
I need to use adCmdText instead of adCmdTable to set query as a
source. in the rs->Open(....
Thanx for the reply.
From: Giovanni Dicanio on

"NightCrawler" <muley.rahul(a)gmail.com> ha scritto nel messaggio
news:a33fb82d-d840-43b0-b932-c91228068759(a)u29g2000pro.googlegroups.com...

> There is no problem with CString::GetBuffer() it will just return
> the buffer which can easily be typecasted to _bstr_t, I am sure about
> it.

I don't use GetBuffer much... but I thought that GetBuffer required a
ReleaseBuffer call, too, that I did not read in the posted code.

However, I'm glad that the problem is solved in another way.

Giovanni