解决方法
我将假设sql Server CE 3.0 / 3.1但是,供您参考,这里是我知道的提供程序字符串:
> sql Server CE 3.0 / 3.1 – Microsoft.sqlLITE.MOBILE.OLEDB.3.0
> sql Server CE 3.5 – Microsoft.sqlSERVER.CE.OLEDB.3.5
另外,我将为您提供两个代码示例:
>“SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES”上的ICommandText
> DBSCHEMA_TABLES上的IDBSchemaRowset
我正在为您提供样本作为C控制台应用程序,并使用ATL来缩短代码.代码完整且有效,但不包括HRESULT hr的必要错误检查.
这是用于查询sql Server CE 3.0 / 3.1数据库以使用ICommandText和IRowset执行“SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES”的C控制台应用程序示例:
#include <stdio.h> #include <tchar.h> #include <windows.h> #include <oledb.h> #include <atlbase.h> int _tmain(int argc,_TCHAR* argv[]) { HRESULT hr = S_OK; hr = CoInitializeEx(NULL,COINIT_MULTITHREADED); // Connect to sql Server. CComPtr<IDBInitialize> spIDBInitialize; hr = spIDBInitialize.CoCreateInstance(OLESTR("Microsoft.sqlLITE.MOBILE.OLEDB.3.0")); CComPtr<IDBProperties> spIDBProperties; hr = spIDBInitialize->QueryInterface(&spIDBProperties); CComVariant varDataSource(OLESTR("InsertYourSampleDatabase.SDF")); DBPROP prop = { DBPROP_INIT_DATASOURCE,DBPROPOPTIONS_required,DB_NULLID,varDataSource }; DBPROPSET propSet = {&prop,1,DBPROPSET_DBINIT}; hr = spIDBProperties->SetProperties(1,&propSet); spIDBProperties = NULL; hr = spIDBInitialize->Initialize(); // Execute the query. CComPtr<IDBCreateSession> spIDBCreateSession; hr = spIDBInitialize->QueryInterface(&spIDBCreateSession); CComPtr<IDBCreateCommand> spIDBCreateCommand; hr = spIDBCreateSession->CreateSession(NULL,IID_IDBCreateCommand,(IUnknown**) &spIDBCreateCommand); spIDBCreateSession = NULL; CComPtr<ICommandText> spICommandText; hr = spIDBCreateCommand->CreateCommand(NULL,IID_ICommandText,(IUnknown**) &spICommandText); spIDBCreateCommand = NULL; hr = spICommandText->SetCommandText(DBGUID_sql,OLESTR("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES")); DBROWCOUNT cRowsAffected = 0; CComPtr<IRowset> spIRowset; hr = spICommandText->Execute(NULL,IID_IRowset,NULL,&cRowsAffected,(IUnknown**) &spIRowset); spICommandText = NULL; // Retrieve records. HROW hRow = NULL; HROW *rghRow = &hRow; DBCOUNTITEM cRowsObtained = 0; hr = spIRowset->GetNextRows(DB_NULL_HCHAPTER,&cRowsObtained,&rghRow); while (hr == S_OK && cRowsObtained == 1) { // Fetch the TABLE_NAME field. struct { DBSTATUS dbTableNameStatus; ULONG nTableNameLength; WCHAR szTableName[128 + 1]; } data = {0}; DBBINDING Binding = {0}; Binding.iOrdinal = 1; Binding.obValue = sizeof(DBSTATUS) + sizeof(ULONG); Binding.obLength = sizeof(DBSTATUS); Binding.obStatus = 0; Binding.pTypeInfo = NULL; Binding.pObject = NULL; Binding.pBindExt = NULL; Binding.dwPart = DBPART_STATUS | DBPART_LENGTH | DBPART_VALUE; Binding.dwMemOwner = DBMEMOWNER_CLIENTOWNED; Binding.eParamIO = DBPARAMIO_NOTPARAM; Binding.cbMaxLen = (128 + 1) * sizeof(WCHAR); Binding.wType = DBTYPE_WSTR; Binding.dwFlags = 0; Binding.bPrecision = 0; Binding.bScale = 0; CComPtr<IAccessor> spIAccessor; hr = spIRowset->QueryInterface(IID_IAccessor,(void**) &spIAccessor); HACCESSOR hAccessor = NULL; hr = spIAccessor->CreateAccessor(DBACCESSOR_ROWDATA,&Binding,&hAccessor,NULL); hr = spIRowset->GetData(hRow,hAccessor,&data); DBREFCOUNT cRefCount = 0; hr = spIAccessor->ReleaseAccessor(hAccessor,&cRefCount); spIAccessor = NULL; // @@TODO: Do something with data.szTableName and data.nTableNameLength _tprintf(_T("%s\n"),data.szTableName); // Fetch next row of data. hr = spIRowset->ReleaseRows(1,rghRow,NULL); cRowsObtained = 0; hr = spIRowset->GetNextRows(DB_NULL_HCHAPTER,&rghRow); } // Release everything spIRowset = NULL; spIDBInitialize = NULL; CoUninitialize(); return 0; }
这是用于查询sql Server CE 3.0 / 3.1数据库以使用IDBSchemaRowset和IRowset浏览表的C控制台应用程序示例(请注意,TABLE_NAME现在位于iOrdinal 3而不是1):
#include <stdio.h> #include <tchar.h> #include <windows.h> #include <oledb.h> #include <atlbase.h> int _tmain(int argc,&propSet); spIDBProperties = NULL; hr = spIDBInitialize->Initialize(); // Execute the query. CComPtr<IDBCreateSession> spIDBCreateSession; hr = spIDBInitialize->QueryInterface(&spIDBCreateSession); CComPtr<IDBSchemaRowset> spIDBSchemaRowset; hr = spIDBCreateSession->CreateSession(NULL,IID_IDBSchemaRowset,(IUnknown**) &spIDBSchemaRowset); spIDBCreateSession = NULL; CComVariant rgRestrictions[CRESTRICTIONS_DBSCHEMA_TABLES]; // rgRestrictions[2] = OLESTR("CENSUS"); TABLE_NAME restriction rgRestrictions[3] = OLESTR("TABLE"); // TABLE_TYPE restriction CComPtr<IRowset> spIRowset; hr = spIDBSchemaRowset->GetRowset(NULL,DBSCHEMA_TABLES,CRESTRICTIONS_DBSCHEMA_TABLES,rgRestrictions,(IUnknown**) &spIRowset); spIDBSchemaRowset = NULL; // Retrieve records. HROW hRow = NULL; HROW *rghRow = &hRow; DBCOUNTITEM cRowsObtained = 0; hr = spIRowset->GetNextRows(DB_NULL_HCHAPTER,&rghRow); while (hr == S_OK && cRowsObtained == 1) { // Fetch the TABLE_NAME field. struct { DBSTATUS dbTableNameStatus; ULONG nTableNameLength; WCHAR szTableName[128 + 1]; } data = {0}; DBBINDING Binding = {0}; Binding.iOrdinal = 3; Binding.obValue = sizeof(DBSTATUS) + sizeof(ULONG); Binding.obLength = sizeof(DBSTATUS); Binding.obStatus = 0; Binding.pTypeInfo = NULL; Binding.pObject = NULL; Binding.pBindExt = NULL; Binding.dwPart = DBPART_STATUS | DBPART_LENGTH | DBPART_VALUE; Binding.dwMemOwner = DBMEMOWNER_CLIENTOWNED; Binding.eParamIO = DBPARAMIO_NOTPARAM; Binding.cbMaxLen = (128 + 1) * sizeof(WCHAR); Binding.wType = DBTYPE_WSTR; Binding.dwFlags = 0; Binding.bPrecision = 0; Binding.bScale = 0; CComPtr<IAccessor> spIAccessor; hr = spIRowset->QueryInterface(IID_IAccessor,&rghRow); } // Release everything spIRowset = NULL; spIDBInitialize = NULL; CoUninitialize(); return 0; }