使用OLE DB在SQL紧凑型服务器中显示表名的原始C代码

有没有人有一个示例代码,给定数据库文件显示所有用户表的名称

我对.NET代码不感兴趣,只是C语言.

我试图让OLE DB完成任务 – 相比之下,火箭科学似乎是儿童游戏.

解决方法

我将假设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;
}

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...