Hi,
I am using the below OLEDB program to connect with SQL server 2012 and execute commands on the database.
All the queries except DROP TABLE works fine. When DROP TABLE command is executed the below program aborts while the ICommandText::Execute is executed.
Please provide me suggestions to handle the 'DROP TABLE' command. Any way to find that the ICommandText::Execute has executed the 'DROP' command ? Any properties to be set?
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <oledb.h>
#include <atlbase.h>
#include <stddef.h>
#include <oledberr.h>
#include <sqlncli.h>
void main(int argc, char* argv[])
{
IUnknown * pIUnknown ;
BOOL * pbValue = FALSE;
CLSID clsid;
HRESULT hr;
IDBInitialize * pIDBInitialize ;
IDBProperties* pIDBProperties ;
IMultipleResults* pIMultipleResults;
IRowset* pIRowset;
IUnknown * g_pISession;
hr = CoInitialize(NULL);
char *pSQLNCLI_progidName = NULL;
pSQLNCLI_progidName = strdup("SQLNCLI10");
int lenA = lstrlenA(pSQLNCLI_progidName);
int lenW = 0;
BSTR sqlncli_progidW = NULL;
lenW = ::MultiByteToWideChar(CP_ACP, 0, pSQLNCLI_progidName, lenA, 0, 0);
if (lenW > 0)
{
// Check whether conversion was successful
sqlncli_progidW = ::SysAllocStringLen(0, lenW);
::MultiByteToWideChar(CP_ACP, 0, pSQLNCLI_progidName, lenA, sqlncli_progidW,
lenW);
}
else
{
fprintf(stderr," *** ERROR *** progid conversion failure from char
to Wchar \n");
}
// retrieve the clsid from register by program name, SQLNCLI11(2012) or SQLNCLI10(2008)
CLSID clsidSQLNCLI = CLSID_SQLNCLI;
hr = CLSIDFromProgID(sqlncli_progidW, &clsidSQLNCLI);
hr = CoCreateInstance( clsidSQLNCLI,NULL,CLSCTX_INPROC_SERVER,IID_IDBInitialize,(void **) &pIDBInitialize);
fprintf(stderr, "Instance created \n" );
const ULONG nProps = 4;
DBPROP InitProperties[nProps];
DBPROPSET rgInitPropSet[1];
for (ULONG i = 0; i < nProps; i++)
{
VariantInit(&InitProperties[i].vValue);
InitProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
InitProperties[i].colid = DB_NULLID;
}
InitProperties[0].dwPropertyID
= DBPROP_INIT_DATASOURCE;
InitProperties[0].vValue.vt
= VT_BSTR;
InitProperties[0].vValue.bstrVal = SysAllocString(OLESTR("MACHINE\\MSSQLSERVER11"));
InitProperties[0].dwOptions
= DBPROPOPTIONS_REQUIRED;
InitProperties[0].colid
= DB_NULLID;
InitProperties[0].dwStatus
= DBPROPSTATUS_OK;
//Specify database name.
InitProperties[1].dwPropertyID
= DBPROP_INIT_CATALOG;
InitProperties[1].vValue.vt
= VT_BSTR;
InitProperties[1].vValue.bstrVal = SysAllocString(OLESTR("TEST"));
InitProperties[1].dwOptions
= DBPROPOPTIONS_REQUIRED;
InitProperties[1].colid
= DB_NULLID;
//Specify user name (login).
InitProperties[2].dwPropertyID
= DBPROP_AUTH_USERID;
InitProperties[2].vValue.vt
= VT_BSTR;
InitProperties[2].vValue.bstrVal = SysAllocString(OLESTR("sa"));
InitProperties[2].dwOptions
= DBPROPOPTIONS_REQUIRED;
InitProperties[2].colid
= DB_NULLID;
//Specify password.
InitProperties[3].dwPropertyID
= DBPROP_AUTH_PASSWORD;
InitProperties[3].vValue.vt
= VT_BSTR;
InitProperties[3].vValue.bstrVal = SysAllocString(OLESTR("Password"));
InitProperties[3].dwOptions
= DBPROPOPTIONS_REQUIRED;
InitProperties[3].colid
= DB_NULLID;
//Assign the property structures to the property set.
rgInitPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
rgInitPropSet[0].cProperties = nProps;
rgInitPropSet[0].rgProperties = InitProperties;
hr = pIDBInitialize->QueryInterface(IID_IDBProperties, (void **)&pIDBProperties);
hr = pIDBProperties->SetProperties(1, rgInitPropSet);
pIDBProperties->Release();
ULONG i; //Infosys:64-bit change:i declared out of for loop as ULONG
for( i=0; i<nProps; i++ )
{
if (InitProperties[ i ].vValue.vt == VT_BSTR)
SysFreeString( InitProperties[ i ].vValue.bstrVal );
}
hr = pIDBInitialize->Initialize();
if (FAILED(hr)) {
fprintf(stderr,"initialize failed %X\n", hr );
return;
}
//These variables are for Command creation.
IDBCreateSession* pIDBCreateSession;
IDBCreateCommand* pIDBCreateCommand;
ICommandText* pICommandText;
DBROWCOUNT cRowsAffected;
//Get the DB Session object.
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,(void**)&pIDBCreateSession);
if (FAILED(hr)){
fprintf(stderr,"Query:create session failed \n" );
return;
}
//Create the session, and get an interface for command creation.
hr = pIDBCreateSession->CreateSession(NULL,IID_IOpenRowset, (IUnknown**)&g_pISession);
if (FAILED(hr)){
fprintf(stderr,"create session failed \n" );
return;
}
hr = g_pISession->QueryInterface(IID_IDBCreateCommand, (void**)&pIDBCreateCommand);
//Create the command object.
hr = pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText,(IUnknown**)&pICommandText);
pIDBCreateCommand->Release();
if (FAILED(hr))
return;
// hr = pICommandText->SetCommandText(DBGUID_DBSQL, L"select * from TestTable");
//hr = pICommandText->SetCommandText(DBGUID_DBSQL, L"INSERT INTO TestTable(ID) VALUES ('2377434')");
//hr = pICommandText->SetCommandText(DBGUID_DBSQL, L"DELETE FROM TestTable WHERE ID = '2377434'");
hr = pICommandText->SetCommandText(DBGUID_DBSQL, L"DROP TABLE TestTable");
//Execute the command.
hr = pICommandText->Execute( NULL,IID_IRowset,NULL,&cRowsAffected,(IUnknown **) &pIRowset );
if(hr != S_OK)
{
fprintf(stderr," Error - execute \n");
}
else{
if (cRowsAffected > 0)
{
fprintf(stderr," Updated rows = %d \n" , cRowsAffected);
return;
}
}
// Retrieve records.
HROW hRow = NULL;
HROW *rghRow = &hRow;
DBCOUNTITEM cRowsObtained = 0;
hr = pIRowset->GetNextRows(DB_NULL_HCHAPTER, 0, 1, &cRowsObtained, &rghRow);
fprintf(stderr, " Selected Rows = %d \n" , cRowsObtained);
return;
}
Thanks in advance