SmileeTiger
Programmer
I am trying to open a access database and update a record using the following code but it seems to fail when I execute the SQL statement. Any idea what's the matter?
It fails on the line pRecordset->Open(bstrtSQL, vtMissing, adOpenForwardOnly,adLockOptimistic, adCmdUnknown);
void CRecoveringSeatsandReservationsDlg::OnRecover()
{
//Connecting to the database
CoInitialize (NULL);
_ConnectionPtr m_pConn;
HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection));
if (FAILED( hr ))
AfxMessageBox( "Can't create an intance of ADO.Connection" );
else
{
if (FAILED( m_pConn->Open(
_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:\\Recovering\\Recovering Seats and Reservations\\Debug\\MainDatabase.mdb"
,
_bstr_t( "" ), _bstr_t( "" ), adModeUnknown )))
AfxMessageBox( "Can't open datasource" ); //Couldn't connect to data source
//end of connecting
//Setting up SQL command
_CommandPtr pCommand;
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConn; // Formerly opened connection pointer
//pCommand->CommandText = "INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')";
CString Test="INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')";
//Running the command
_RecordsetPtr pRecordset;
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
CString zPath;
CString zSQL;
CString zPathToBackroomDB;
CString zIndex, zNode, zTemp;
_bstr_t bstrtSQL;
BSTR bstrValue;
zSQL= CString ("INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')"
;
bstrValue = zSQL.AllocSysString();
bstrtSQL = bstrValue;
bstrValue = NULL;
pRecordset->PutRefActiveConnection(m_pConn);
pRecordset->Open(bstrtSQL, vtMissing, adOpenForwardOnly,adLockOptimistic, adCmdUnknown);
AfxMessageBox( "YEA" );
m_pConn->Close(); //Closingconnection
}
}
It fails on the line pRecordset->Open(bstrtSQL, vtMissing, adOpenForwardOnly,adLockOptimistic, adCmdUnknown);
void CRecoveringSeatsandReservationsDlg::OnRecover()
{
//Connecting to the database
CoInitialize (NULL);
_ConnectionPtr m_pConn;
HRESULT hr = m_pConn.CreateInstance(__uuidof(Connection));
if (FAILED( hr ))
AfxMessageBox( "Can't create an intance of ADO.Connection" );
else
{
if (FAILED( m_pConn->Open(
_bstr_t("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = E:\\Recovering\\Recovering Seats and Reservations\\Debug\\MainDatabase.mdb"
_bstr_t( "" ), _bstr_t( "" ), adModeUnknown )))
AfxMessageBox( "Can't open datasource" ); //Couldn't connect to data source
//end of connecting
//Setting up SQL command
_CommandPtr pCommand;
pCommand.CreateInstance (__uuidof (Command));
pCommand->ActiveConnection = m_pConn; // Formerly opened connection pointer
//pCommand->CommandText = "INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')";
CString Test="INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')";
//Running the command
_RecordsetPtr pRecordset;
pRecordset.CreateInstance (__uuidof (Recordset));
pRecordset->CursorLocation = adUseClient;
CString zPath;
CString zSQL;
CString zPathToBackroomDB;
CString zIndex, zNode, zTemp;
_bstr_t bstrtSQL;
BSTR bstrValue;
zSQL= CString ("INSERT INTO User (UserID, Name) VALUES (1, 'Storgt')"
bstrValue = zSQL.AllocSysString();
bstrtSQL = bstrValue;
bstrValue = NULL;
pRecordset->PutRefActiveConnection(m_pConn);
pRecordset->Open(bstrtSQL, vtMissing, adOpenForwardOnly,adLockOptimistic, adCmdUnknown);
AfxMessageBox( "YEA" );
m_pConn->Close(); //Closingconnection
}
}