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.
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.
> 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