📄 sqlcesvr.cpp
字号:
// SqlceSvr.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "SqlceSvr.h"
#define NUMROWS_CHUNK 7200
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//extern HWND hWndList1;
//extern int UpdateList (LPCTSTR);
// This is an example of an exported variable
SQLCESVR_API int nSqlceSvr=0;
// This is an example of an exported function.
SQLCESVR_API int fnSqlceSvr(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see SqlceSvr.h for the class definition
CSqlceSvr::CSqlceSvr()
{
return;
}
/*extern "C" SQLCESVR_API int testdll(int a,int b)
{
return a+b;
}*/
extern "C" SQLCESVR_API HRESULT Init(void)
{
HRESULT hr = NOERROR;
hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
return hr;
}
extern "C" SQLCESVR_API void UnInit(void)
{
CoUninitialize();
}
extern "C" SQLCESVR_API HRESULT CreateSqlSvrCeProvider(void)
{
HRESULT hr = NOERROR;
hr = CoCreateInstance(CLSID_SQLSERVERCE_2_0,
0,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void**)&pIDBInitialize);
//::MessageBox(NULL,_T("testdll"),_T("testdll"),MB_OK);
return hr;
}
extern "C" SQLCESVR_API HRESULT CreateDBSession (void)
{
HRESULT hr = NOERROR;
// Query the IDBCreateSession interface
hr = pIDBInitialize->QueryInterface(IID_IDBCreateSession,
(void **)&pIDBCreateSession);
if(FAILED(hr))
goto CleanExit;
// 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;
// Create a command object pointer
hr = pIDBCrtCmd->CreateCommand(NULL,
IID_ICommandText,
(IUnknown**)&pICmdText);
if(FAILED(hr))
goto CleanExit;
CleanExit:
return hr;
}
extern "C" SQLCESVR_API HRESULT ConnectDB (LPTSTR lpszDBName)
{
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;
// 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;
}
extern "C" SQLCESVR_API 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 != pICmdText)
{
pICmdText->Release();
pICmdText = NULL;
}
if(NULL != pIDBCrtCmd)
{
pIDBCrtCmd->Release();
pIDBCrtCmd = NULL;
}
// Release interfaces
if(NULL != pIDBInitialize)
{
pIDBInitialize->Release();
pIDBInitialize = NULL;
}
return S_OK;
}
extern "C" SQLCESVR_API HRESULT CreateDB (LPTSTR lpszDBName)
{
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;
// 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;
}
extern "C" SQLCESVR_API HRESULT DeleteDB (LPTSTR lpszDBName)
{
if (DeleteFile(lpszDBName) > 0)
return S_OK;
else
return E_FAIL;
}
extern "C" SQLCESVR_API 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;
// 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -