Algun experto en SQL y c++ que se atreva aportarla a FWPPC?.
Code: Select all
//***********************************************************
// SQL Server CE Interface Function //
// //
// Author : Chris C //
// PassportONE.com //
// http://www.PassportONE.com //
// Email: chris@PassportONE.com //
// © Aug 2002 //
// //
//***********************************************************
#include "SqlSvrCe.h"
// -------------------------------------------------------
//
// SQL SERVER CE INTERFACE FUNCTION
//
// -------------------------------------------------------
HRESULT CreateSqlSvrCeProvider (void)
{
// PURPOSE:
// - Initlialize the OLE DB Provider...
// PARAMETERS:
// - NIL
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
// Create the SQL Server CE provider
hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0,
0,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void**)&pIDBInitialize);
return hr;
}
HRESULT CreateDBSession (void)
{
// PURPOSE:
// - Create a new SQL Server CE Database session of the connected database...
// PARAMETERS:
// - NULL
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
// Query the IDBCreateSession interface
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,
(void **)&pIDBCreateSession);
if(FAILED(hr))
goto CleanExit;
else
pIDBInitialize->AddRef();
// Create a new database session...
hr = pIDBCreateSession->CreateSession(NULL,
IID_IUnknown,
&pIUnknownSession);
if(FAILED(hr))
goto CleanExit;
// Query the IDBCreateCommand interface
hr = pIUnknownSession->QueryInterface(IID_IDBCreateCommand,
(void**)&pIDBCrtCmd);
if(FAILED(hr))
goto CleanExit;
else
pIUnknownSession->AddRef();
// Create a command object pointer
hr = pIDBCrtCmd->CreateCommand(NULL,
IID_ICommandText,
(IUnknown**)&pICmdText);
if(FAILED(hr))
goto CleanExit;
CleanExit:
return hr;
}
HRESULT ConnectDB (LPTSTR lpszDBName)
{
// PURPOSE:
// - Connect the given SQL Server CE Database
// PARAMETERS:
// - lpszDBName :: SQL Server CE Database filename in fullpath
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
DBPROPSET dbpropset[1]; // Property Set used to initialize provider
DBPROP dbprop[1]; // property array used in property set to initialize provider
// Create the SQL Server CE provider
hr = CreateSqlSvrCeProvider();
// Validation
if(FAILED(hr))
goto CleanExit;
// Initialize...
VariantInit(&dbprop[0].vValue);
// Initialize a property with name of database
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(lpszDBName);
// Validation
if(NULL == dbprop[0].vValue.bstrVal)
{
// Set return value
hr = E_OUTOFMEMORY;
goto CleanExit;
}
// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
// Query the IDBProperties interface
hr = pIDBInitialize->QueryInterface(IID_IDBProperties,
(void **)&pIDBProperties);
// Validation
if(FAILED(hr))
goto CleanExit;
else
pIDBInitialize->AddRef();
// Create the given database...
hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]),
dbpropset);
// Validation
if(FAILED(hr))
goto CleanExit;
// Free the used memory
SysFreeString(dbprop[0].vValue.bstrVal);
// Initialize the SQL Server CE provider.
pIDBInitialize->Initialize();
// Create new database session...
hr = CreateDBSession();
CleanExit:
// Release the used memory
VariantClear(&dbprop[0].vValue);
// Only execute the following command when either one of the
// above command fail.
if (FAILED(hr))
// Disconnect the database/reset the OLE DB variable
DisconnectDB(lpszDBName);
return hr;
}
HRESULT DisconnectDB (LPTSTR lpszDBName)
{
// PURPOSE:
// - Disconnect from the given SQL Server CE Database
// PARAMETERS:
// - lpszDBName :: SQL Server CE Database filename in fullpath
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
if(NULL != pIDBCreateSession)
{
pIDBCreateSession->Release();
pIDBCreateSession = NULL;
}
if(NULL != pIUnknownSession)
{
pIUnknownSession->Release();
pIUnknownSession = NULL;
}
if(NULL != pIDBProperties)
{
pIDBProperties->Release();
pIDBProperties = NULL;
}
if(NULL != pIDBCrtCmd)
{
pIDBCrtCmd->Release();
pIDBCrtCmd = NULL;
}
if(NULL != pICmdText)
{
pICmdText->Release();
pICmdText = NULL;
}
// Release interfaces
if(NULL != pIDBInitialize)
{
pIDBInitialize->Release();
pIDBInitialize = NULL;
}
return S_OK;
}
HRESULT CreateDB (LPTSTR lpszDBName)
{
// PURPOSE:
// - Create a new SQL Server CE Database with the given name
// PARAMETERS:
// - lpszDBName :: SQL Server CE Database filename in fullpath
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
DBPROPSET dbpropset[1]; // Property Set used to initialize provider
DBPROP dbprop[1]; // property array used in property set to initialize provider
IDBDataSourceAdmin *pIDBDataSourceAdmin = NULL;
// Create the SQL Server CE provider
hr = CreateSqlSvrCeProvider();
// Validation
if(FAILED(hr))
goto CleanExit;
// Initialize...
VariantInit(&dbprop[0].vValue);
// Initialize a property with name of database
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(lpszDBName);
// Validation
if(NULL == dbprop[0].vValue.bstrVal)
{
// Set return value
hr = E_OUTOFMEMORY;
goto CleanExit;
}
// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
// Query the IDBDataSourceAdmin interface
hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin,
(void **)&pIDBDataSourceAdmin);
// Validation
if(FAILED(hr))
goto CleanExit;
else
pIDBInitialize->AddRef();
// Create the given database...
hr = pIDBDataSourceAdmin->CreateDataSource(1,
dbpropset,
NULL,
IID_IDBProperties,
NULL);
// Validation
if(FAILED(hr))
// Clean the memory...
goto CleanExit;
// Free the used memory
SysFreeString(dbprop[0].vValue.bstrVal);
// Create new database session...
hr = CreateDBSession();
CleanExit:
// Release the used memory
VariantClear(&dbprop[0].vValue);
// Release the OLE DB interface
if(NULL != pIDBDataSourceAdmin)
{
pIDBDataSourceAdmin->Release();
pIDBDataSourceAdmin = NULL;
}
// Only execute the following command when either one of the
// above command fail.
if (FAILED(hr))
// Disconnect the database/reset the OLE DB variable
DisconnectDB(lpszDBName);
return hr;
}
HRESULT DeleteDB (LPTSTR lpszDBName)
{
// PURPOSE:
// - Delete an existing SQL Server CE Database with the given name
// PARAMETERS:
// - lpszDBName :: SQL Server CE Database filename in fullpath
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
if (DeleteFile(lpszDBName) > 0)
return S_OK;
else
return E_FAIL;
}
HRESULT CompactDB (LPTSTR lpszDBName)
{
// PURPOSE:
// - Compact an existing SQL Server CE Database with the given name
// PARAMETERS:
// - lpszDBName :: SQL Server CE Database filename in fullpath
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
TCHAR szNewDBName[256];
// Property Set used to initialize provider
DBPROPSET compactdbpropset[1];
DBPROPSET dbpropset[1];
// Property array used in property set to initialize provider
DBPROP compactdbprop[1];
DBPROP dbprop[1];
ISSCECompact *pISSCECompact = NULL;
// Create the SQL Server CE provider
hr = CreateSqlSvrCeProvider();
// Validation
if(FAILED(hr))
goto CleanExit;
// Initialize...
VariantInit(&dbprop[0].vValue);
// Initialize a property with name of database
dbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
dbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
dbprop[0].vValue.vt = VT_BSTR;
dbprop[0].vValue.bstrVal = SysAllocString(lpszDBName);
// Validation
if(NULL == dbprop[0].vValue.bstrVal)
{
// Set return value
hr = E_OUTOFMEMORY;
goto CleanExit;
}
// Initialize the property set
dbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
dbpropset[0].rgProperties = dbprop;
dbpropset[0].cProperties = sizeof(dbprop)/sizeof(dbprop[0]);
// Query the IDBProperties interface
hr = pIDBInitialize->QueryInterface(IID_IDBProperties,
(void **)&pIDBProperties);
// Validation
if(FAILED(hr))
goto CleanExit;
else
pIDBInitialize->AddRef();
// Create the given database...
hr = pIDBProperties->SetProperties(sizeof(dbpropset)/sizeof(dbpropset[0]),
dbpropset);
// Validation
if(FAILED(hr))
goto CleanExit;
// Free the used memory
SysFreeString(dbprop[0].vValue.bstrVal);
// Initialize the SQL Server CE provider.
pIDBInitialize->Initialize();
// Get ISSCECompact interface
hr = pIDBProperties->QueryInterface(IID_ISSCECompact,
(void **)&pISSCECompact);
// Validation
if(FAILED(hr))
goto CleanExit;
else
pIDBProperties->AddRef();
// Initialize Property with name of new compacted database
compactdbprop[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
compactdbprop[0].dwOptions = DBPROPOPTIONS_REQUIRED;
compactdbprop[0].vValue.vt = VT_BSTR;
// Create new database name
memset(szNewDBName, TEXT('\0'), 256);
wsprintf(szNewDBName, TEXT("%sx"), lpszDBName);
// new name for compacted database
compactdbprop[0].vValue.bstrVal = SysAllocString(szNewDBName);
// Initialize property set
compactdbpropset[0].guidPropertySet = DBPROPSET_DBINIT;
compactdbpropset[0].rgProperties = compactdbprop;
compactdbpropset[0].cProperties = sizeof(compactdbprop)/sizeof(compactdbprop[0]);
// Compact the database using the provider-specific interface
hr = pISSCECompact->Compact(sizeof(compactdbpropset)/sizeof(compactdbpropset[0]),
compactdbpropset);
// Release the interface
pISSCECompact->Release();
pISSCECompact = NULL;
// Disconnect the database/reset the OLE DB variable
DisconnectDB(lpszDBName);
// Overwrite the original database
if (!CopyFile(szNewDBName, lpszDBName, false))
{
// Set the return value
hr = E_FAIL;
goto CleanExit;
}
// Delete the temp database...
DeleteDB(szNewDBName);
CleanExit:
// Release the used memory
VariantClear(&dbprop[0].vValue);
// Only execute the following command when either one of the
// above command fail.
if (FAILED(hr))
// Disconnect the database/reset the OLE DB variable
DisconnectDB(lpszDBName);
return hr;
}
HRESULT CreateTable (LPTABLECOLUMNINFO lptci)
{
// PURPOSE:
// - Create new table with the specified table name.
// PARAMETERS:
// - lptci :: Target column information.
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
// CREATE TABLE ABC (MyField INT IDENTITY (s,i) NOT NULL CONSTRAINT PK_MyField PRIMARY KEY [UNIQUE])
TCHAR szBuffer[2048] = {NULL};
TCHAR szSQL92[2048] = {NULL};
// Get the SQL92 command
GetSQL92ColumnDef(lptci, (LPTSTR)&szSQL92, 2048);
// Compose the SQL-92 command
wsprintf(szBuffer,
TEXT("CREATE TABLE %s (%s)"),
lptci->szTableName, szSQL92);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT DropTable (LPTSTR lpszTableName)
{
// PURPOSE:
// - Drop the existing table with the specified table name
// PARAMETERS:
// - lpszTableName :: Existing target table name.
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
TCHAR szBuffer[2048] = {NULL};
// Compose the SQL-92 command
wsprintf(szBuffer,
TEXT("DROP TABLE %s"),
lpszTableName);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT CreateColumn (LPTABLECOLUMNINFO lptci)
{
// PURPOSE:
// - Create new column with the specified column information
// PARAMETERS:
// - lptci :: Target column information.
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
TCHAR szBuffer[2048] = {NULL};
TCHAR szSQL92[2048] = {NULL};
// Get the SQL92 command
GetSQL92ColumnDef(lptci, (LPTSTR)&szSQL92, 2048);
// Compose the SQL-92 command
wsprintf(szBuffer,
TEXT("ALTER TABLE %s ADD %s"),
lptci->szTableName, szSQL92);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT DropColumn (LPTABLECOLUMNINFO lptci)
{
// PURPOSE:
// - Drop the existing column with the specified column information
// PARAMETERS:
// - lptci :: Target column information..
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
TCHAR szBuffer[2048] = {NULL};
// Compose the SQL-92 command
wsprintf(szBuffer,
TEXT("ALTER TABLE %s DROP COLUMN %s"),
lptci->szTableName, lptci->szColumnName);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT CreateIndex (LPTABLEINDEXINFO lptii)
{
// PURPOSE:
// - Create new index with the specified index information
// PARAMETERS:
// - lptii :: 32-Bits long pointer point to TABLEINDEXINFO structure
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
TCHAR szBuffer[2048] = {NULL};
TCHAR szUnique[8] = {NULL};
// Validate the Unique mode.
if (lptii->bUnique)
_tcscpy(szUnique, TEXT("UNIQUE"));
else
_tcscpy(szUnique, TEXT(""));
// Compose the SQL-92 command
// NOTE:
// DUE TO SQL CE 2.0 DOES NOT SUPPORT CLUSTERED INDEX,
// HENCE, NONCLUSTERED KEYWORD IS APPLIED IN THIS CASE.
wsprintf(szBuffer,
TEXT("CREATE %s NONCLUSTERED INDEX %s ON %s (%s)"),
szUnique, lptii->szIndexName, lptii->szTableName, lptii->szIndexFields);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT DropIndex (LPTABLEINDEXINFO lptii)
{
// PURPOSE:
// - Drop the existing index with the specified index information
// PARAMETERS:
// - lptii :: 32-Bits long pointer point to TABLEINDEXINFO structure
// OPERATION:
// - Compose SQL-92 command
// - Execute the SQL-92 command with ExecuteSQL interface
// RETURN VALUE:
// - HRESULT
TCHAR szBuffer[2048] = {NULL};
// Compose the SQL-92 command
wsprintf(szBuffer,
TEXT("DROP INDEX %s.%s"),
lptii->szTableName, lptii->szIndexName);
// Execute the SQL-92 command
return ExecuteSQL(szBuffer);
}
HRESULT GetSQL92ColumnDef (LPTABLECOLUMNINFO lptci, LPTSTR lpszBuffer, int nMaxBuffer)
{
// PURPOSE:
// - Build the SQL92 comand for ALTER/CRAETE column statement
// PARAMETERS:
// - lptci :: Target column information.
// - lpszbuffer :: Output buffer
// - nMaxBuffer :: Output buffer size
// OPERATION:
// - Check through the pass in column information and build the respective
// SQL92 command
// RETURN VALUE:
// - HRESULT
TCHAR szColumnType[64] = {NULL};
TCHAR szIdentity[32] = {NULL};
TCHAR szAcceptNull[16] = {NULL};
TCHAR szPrimaryKey[64] = {NULL};
TCHAR szUnique[8] = {NULL};
// Check user define column type
{
switch (lptci->nColumnType)
{
case 0:
// int
_tcscpy(szColumnType, TEXT("INT"));
break;
case 1:
// smallint
_tcscpy(szColumnType, TEXT("SMALLINT"));
break;
case 2:
// tinyint
_tcscpy(szColumnType, TEXT("TINYINT"));
break;
case 3:
// bigint
_tcscpy(szColumnType, TEXT("BIGINT"));
break;
case 4:
// float
_tcscpy(szColumnType, TEXT("FLOAT"));
break;
case 5:
// nvarchar
wsprintf(szColumnType,
TEXT("NVARCHAR (%d)"),
lptci->nColumnSize);
break;
case 6:
// nchar
wsprintf(szColumnType,
TEXT("NCHAR (%d)"),
lptci->nColumnSize);
break;
case 7:
// bit
_tcscpy(szColumnType, TEXT("BIT"));
break;
case 8:
// datetime
_tcscpy(szColumnType, TEXT("DATETIME"));
break;
case 9:
// numeric
_tcscpy(szColumnType, TEXT("NUMERIC"));
break;
default:
// Default empty string
_tcscpy(szColumnType, TEXT(""));
}
}
// Check IDENTITY flag
{
if (lptci->bIdentity)
wsprintf(szIdentity,
TEXT("IDENTITY (%d,%d)"),
HIWORD(lptci->dwIdentityProp), LOWORD(lptci->dwIdentityProp));
else
_tcscpy(szIdentity, TEXT(""));
}
// Check ACCPETNULL flag
{
if (lptci->bAcceptNull)
_tcscpy(szAcceptNull, TEXT("NULL"));
else
_tcscpy(szAcceptNull, TEXT("NOT NULL"));
}
// Check PRIMARY KEY flag
{
if (lptci->bPrimaryKey)
wsprintf(szPrimaryKey,
TEXT(", CONSTRAINT %s PRIMARY KEY (%s)"),
lptci->szPrimaryKeyName, lptci->szColumnName);
else
_tcscpy(szPrimaryKey, TEXT(""));
}
// Check UNIQUE flag
{
if (lptci->bUnique)
_tcscpy(szUnique, TEXT("UNIQUE"));
else
_tcscpy(szUnique, TEXT(""));
}
// initialize the output buffer
memset(lpszBuffer, TEXT('\0'), nMaxBuffer);
// Copy result into the output buffer
wsprintf(lpszBuffer,
TEXT("%s %s %s %s %s %s"),
lptci->szColumnName, szColumnType, szIdentity, szAcceptNull, szPrimaryKey, szUnique);
return NOERROR;
}
HRESULT ExecuteSQL (LPTSTR lpszQuery)
{
// PURPOSE:
// - Execute the given SQL statement.
// PARAMETERS:
// - lpszQuery :: SQL query command string.
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
// Set the SQL query statement
hr = pICmdText->SetCommandText(DBGUID_SQL,
lpszQuery);
if(FAILED(hr))
goto CleanExit;
// Execute the SQL query statement without return any Rowset
hr = pICmdText->Execute(NULL,
IID_NULL,
NULL,
NULL,
NULL);
CleanExit:
return hr;
}
HRESULT GetRowset (LPTSTR lpszQuery)
{
// PURPOSE:
// - Execute the given SQL statement and return a RowSet object.
// PARAMETERS:
// - lpszQuery :: SQL query command string.
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
// NOTE:
// THE FOLLOWING CODE SHOW HOW TO CREATE A ROWSET OBJECT
// WITH COMMAND OBJECT (SQL STATEMENT).
HRESULT hr = NOERROR;
IRowset *pIRowset = NULL;
ICommandText *pICommandText = NULL;
// Create a command object pointer
hr = pIDBCrtCmd->CreateCommand(NULL,
IID_ICommandText,
(IUnknown**)&pICommandText);
// Validation
if(FAILED(hr))
goto CleanExit;
// Set the SQL query statement
hr = pICommandText->SetCommandText(DBGUID_SQL,
lpszQuery);
if(FAILED(hr))
goto CleanExit;
// Execute the SQL query statement
hr = pICommandText->Execute(NULL,
IID_IRowset,
NULL,
NULL,
(IUnknown **)&pIRowset);
if (!FAILED(hr))
{
if(NULL != pIRowset)
// Proceed to walk through the retrieve Rowset object
ProcessRowset(pIRowset);
}
CleanExit:
if (NULL != pIRowset)
{
pIRowset->Release();
pIRowset = NULL;
}
if (NULL != pICommandText)
{
pICommandText->Release();
pICommandText = NULL;
}
return hr;
}
HRESULT ProcessRowset (IRowset *pIRowset)
{
// PURPOSE:
// - Retrieve and display data resulting from
// a query specified in GetRowset function
// PARAMETERS:
// - NIL
// OPERATION:
// - ...
// RETURN VALUE:
// - HRESULT
HRESULT hr = NOERROR;
/*
// FOR LISTVIEW USED ONLY
LVCOLUMN pcol;
LVITEM pitem;
long idx=0;
long lTotalRows = 0;
static long lTotalCols = 0;
long t1=0;
long t2=0;
long elapse=0;
int iHour, iMinute, iSecond, iMiliSec;
ULONG lColumn = 0;
ULONG lNumCols = 0;
ULONG lCount = 0;
ULONG lNumRowsRetrieved = 0;
ULONG ConsumerBufColOffset = 0;
IAccessor *pIAccessor = NULL;
IColumnsInfo *pIColumnsInfo = NULL;
DBCOLUMNINFO *pDBColumnInfo = NULL;
DBBINDING *pBindings = NULL;
HACCESSOR hAccessor = NULL;
HROW hRows[10];
HROW *pRows = &hRows[0];
BYTE *pBuffer = NULL;
WCHAR *pStringsBuffer = NULL;
TCHAR szBuffer[1024];
// NOTE:
// THE FOLLOWING CODE SHOW THE GENERATE METHOD TO DISPLAY
// THE READED ROWSET COLUMN DATA INTO LISTVIEW.
// HENCE, FOR BETTER EFFICIENTCY, THE FOLLOWING CODE
// MUST BE MODIFIED TO SUITE THE NEED WITH REFERENCE TO
// THE RESPECTIVE ROWSET.
// Get the start time (CPU Tick)
t1 = GetTickCount();
// Obtain access to the IColumnInfo interface, from the Rowset object.
hr = pIRowset->QueryInterface(IID_IColumnsInfo,
(void **)&pIColumnsInfo);
// Validation
if(FAILED(hr))
{
// Update status
UpdateList(TEXT("Failed to query IColumnsInfo interface!"));
// Terminate the current routine
goto CleanExit;
}
else
pIRowset->AddRef();
// Retrieve the column information.
pIColumnsInfo->GetColumnInfo(&lNumCols,
&pDBColumnInfo,
&pStringsBuffer);
// Free the column information interface.
pIColumnsInfo->Release();
// Create a DBBINDING array.
pBindings = new DBBINDING[lNumCols];
// Using the ColumnInfo structure, fill out the pBindings array.
for(lCount=0; lCount<lNumCols; lCount++)
{
pBindings[lCount].iOrdinal = lCount+1;
pBindings[lCount].obValue = ConsumerBufColOffset;
pBindings[lCount].pTypeInfo = NULL;
pBindings[lCount].pObject = NULL;
pBindings[lCount].pBindExt = NULL;
pBindings[lCount].dwPart = DBPART_VALUE;
pBindings[lCount].dwMemOwner = DBMEMOWNER_CLIENTOWNED;
pBindings[lCount].eParamIO = DBPARAMIO_NOTPARAM;
// NOTE:
// DUE TO THE OUTPUT DATA TYPE OF EACH FIELDS WAS
// CONVERTED INTO "DBTYPE_WSTR" WITHIN THE SQL SERVER CE
// (HARDCODED) INSTEAD OF USING THE ORIGINAL DATA TYPE AS:
// pBindings[lCount].wType
// AS A RESULT, IT WILL NO LONGER FOLOOW THE VALUE STORE
// IN pDBColumnInfo[lCount].ulColumnSize
//
// HENCE, THE MAXIMUM COLUMN SIZE WAS SET TO 48BYTES
// IT CAN BE ANY VALUE, AS LONG AS IT IS LARGE ENOUGH
// TO HOLD THE CONVERTED DATA FOR ALL THE READ COLUMNS.
pBindings[lCount].cbMaxLen = 48;
pBindings[lCount].dwFlags = 0;
// NOTE:
// DUE TO DATA CONVERSION ERROR, SO WE HARDCODED THE
// DATA TYPE TO DBTYPE_WSTR INSTEAD OF USING THE DATA
// TYPE OBTAIN FROM THE DBCOLUMNINFO STRUCTURE AS:
// DBColumnInfo[lCount].wType
// THROUGH THE GetColumnInfo INTERFACE
pBindings[lCount].wType = DBTYPE_WSTR;
pBindings[lCount].bPrecision = pDBColumnInfo[lCount].bPrecision;
pBindings[lCount].bScale = pDBColumnInfo[lCount].bScale;
// NOTE:
// DUE TO THE DATA TYPE WAS HARDCODED TO DBTYPE_WSTR. HENCE
// THE "ColumnSize" VALUE IN THE DBCOLUMNINFO STRUCTURE AS:
// pDBColumnInfo[lCount].ulColumnSize
// WILL NO LONGER APPLICABLE AND THE NEW HARDCODED SIZE OF
// 48 BYTES WAS USED IN THIS CASE.
// THIS VALUS SHOULD BE CHANGE ARCCODING TO THE DEFINE DATA
// TYPE AND NOT NECESSARY MUST BE 48 BYTES.
// Compute the next buffer offset.
ConsumerBufColOffset += 48; //pDBColumnInfo[lCount].ulColumnSize;
};
// Get the IAccessor interface.
hr = pIRowset->QueryInterface(IID_IAccessor,
(void **)&pIAccessor);
// Validation
if(FAILED(hr))
{
// Update status
UpdateList(TEXT("Failed to query IAccessor interface!"));
// Terminate the current routine
goto CleanExit;
}
else
pIRowset->AddRef();
// Create an accessor from the set of bindings (pBindings).
pIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,
lNumCols,
pBindings,
0,
&hAccessor,
NULL);
// ------------------------------------------------------------
// NOTE:
// THE FOLLOWING CODE CAN BE REMOVE, IF THE COLUMN CAPTION
// DOES NOT REQUIRE.
// ------------------------------------------------------------
// [REMOVABLE CODE - START]
if (lTotalCols > 0)
{
// Clear all the previous listview item
SendMessage(hWndList1, LVM_DELETEALLITEMS, 0, 0);
// Clear the previous column header
for (idx=0; idx<lTotalCols; idx++)
{
SendMessage(hWndList1, LVM_DELETECOLUMN, (int)0, 0);
};
}
// Setup column names.
for(lCount=0; lCount<lNumCols; lCount++)
{
// Initialize listview column structure
pcol.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT;
pcol.fmt = LVCFMT_LEFT;
pcol.pszText = pDBColumnInfo[lCount].pwszName;
//pcol.cx = 80;
pcol.cx = SendMessage(hWndList1, LVM_GETSTRINGWIDTH, 0, (LPARAM)pDBColumnInfo[lCount].pwszName) + 16;
// Append listview column
SendMessage(hWndList1, LVM_INSERTCOLUMN, lCount, (LPARAM)&pcol);
};
//
lTotalCols = lNumCols;
// [REMOVABLE CODE - END]
// Get a set of 10 rows.
pIRowset->GetNextRows(NULL,
0,
10,
&lNumRowsRetrieved,
&pRows);
// Allocate space for the row buffer.
pBuffer = new BYTE[ConsumerBufColOffset];
// Display the rows.
while(lNumRowsRetrieved > 0)
{
//For each row, print the column data.
for(lCount=0; lCount<lNumRowsRetrieved; lCount++)
{
// Initialize...
memset(pBuffer, 0, ConsumerBufColOffset);
// Get the row data values.
pIRowset->GetData(hRows[lCount], hAccessor, pBuffer);
// -- Item
pitem.mask = LVIF_TEXT;
pitem.iItem = SendMessage(hWndList1, LVM_GETITEMCOUNT, 0, 0);
pitem.pszText = (LPTSTR)&pBuffer[pBindings[0].obValue];
pitem.iSubItem = 0;
// Insert item
idx = SendMessage(hWndList1, LVM_INSERTITEM, 0, (LPARAM)&pitem);
// Walk through each columns...
for (lColumn=1; lColumn<lNumCols; lColumn++)
{
pitem.mask = LVIF_TEXT;
pitem.iItem = idx;
pitem.pszText = (LPTSTR)&pBuffer[pBindings[lColumn].obValue];
pitem.iSubItem = lColumn;
// Insert subitem
SendMessage(hWndList1, LVM_SETITEM, 0, (LPARAM)&pitem);
}
// Update the total column counter by 1
lTotalRows++;
};
// Release the rows retrieved.
pIRowset->ReleaseRows(lNumRowsRetrieved,
hRows,
NULL,
NULL,
NULL);
// Get the next set of 10 rows.
pIRowset->GetNextRows(NULL,
0,
10,
&lNumRowsRetrieved,
&pRows);
};
{
// Get the complete time (CPU Tick)
t2 = GetTickCount();
// Calculate the total time
elapse = (t2 - t1)/1000;
iHour = elapse/3600;
iMinute = (elapse/60) - (iHour*60);
iSecond = elapse - (iHour*3600) - (iMinute*60);
iMiliSec = (t2-t1) - (elapse*1000);
// Initialize...
memset(szBuffer, TEXT('\0'), 1024);
// Compose...
wsprintf(szBuffer, TEXT("\r\n(%ld row(s) affected)\r\nElapsed time %02d:%02d:%02d.%03d"), lTotalRows, iHour, iMinute, iSecond, iMiliSec);
// Update Mesasge display
UpdateList(szBuffer);
}
// Set return value
hr = S_OK;
CleanExit:
if (NULL != pIColumnsInfo)
{
pIColumnsInfo->Release();
pIColumnsInfo = NULL;
}
if (NULL != pBindings)
{
delete [] pBindings;
pBindings = NULL;
}
if (NULL != pIAccessor)
{
pIAccessor->Release();
pIAccessor = NULL;
}
/*
if (NULL != pBuffer)
{
delete [] pBuffer;
pBuffer = NULL;
}
*/
return hr;
}