How can I query a MS SQL Compact Server 3.5 database in C++ not using the OLE DB API?

前端 未结 3 717
离开以前
离开以前 2021-01-21 17:43

I have the dlls and the include files of MS SQL Compact Server 3.5. How can I use it without OLE DB? I just want to load the dlls and invoke the necessary methods myself, no COM

3条回答
  •  孤城傲影
    2021-01-21 18:22

    I just noticed you mentioned no COM. If it weren't for that I would have suggested ADO. Recently I posted some OLEDB code for Raw C++ code to display the names of tables in an SQL compact server using OLE DB that you may find useful.

    Otherwise, if you wish want to see my ADODB in C++ answer (that involves COM), I've worked through converting a ADODB VBScript example:

    Dim con, rs
    Set con = CreateObject("ADODB.Connection")
    REM con.Provider = "Microsoft.SQLLITE.MOBILE.OLEDB.3.0"
    con.Provider = "Microsoft.SQLSERVER.CE.OLEDB.3.5"
    con.Open "InsertYourDatabase.sdf"
    Set rs = con.Execute("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES")
    While not rs.EOF
      WScript.Echo rs.Fields.Item(0).Value
      rs.MoveNext
    Wend
    

    To use ADODB in C++ is somewhat arduous, but, it is possible. The following C++ console application shows how to do this by using #import on the ADODB library:

    #include 
    #include 
    #include 
    #include 
    #include 
    #import "c:\Program Files\Common Files\System\ADO\msado15.dll" raw_interfaces_only, raw_native_types, no_namespace, named_guids
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        HRESULT hr = S_OK;
    
        hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    
        // Open a SQL Server CE 3.5 database.
        CComPtr<_Connection> spConnection;
        hr = spConnection.CoCreateInstance(CLSID_Connection);
        //hr = spConnection->put_Provider(CComBSTR(L"Microsoft.SQLLITE.MOBILE.OLEDB.3.0"));
        hr = spConnection->put_Provider(CComBSTR(L"Microsoft.SQLSERVER.CE.OLEDB.3.5"));
        hr = spConnection->Open(CComBSTR(L"InsertYourDatabase.sdf"), CComBSTR(L""), CComBSTR(L""), -1);
    
        // Execute a query.
        CComPtr<_Recordset> spRecordset;
        CComVariant varRecordsAffected;
        hr = spConnection->Execute(CComBSTR(L"SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"), &varRecordsAffected, -1, &spRecordset);
    
        // Fetch the results.
        VARIANT_BOOL bEOF = VARIANT_TRUE;
        hr = spRecordset->get_EOF(&bEOF);
        while (SUCCEEDED(hr) && bEOF != VARIANT_TRUE)
        {
            // Fetch the TABLE_NAME.
            CComPtr spFields;
            hr = spRecordset->get_Fields(&spFields);
            CComPtr spField;
            hr = spFields->get_Item(CComVariant((int) 0), &spField);
            CComVariant varTableName;
            hr = spField->get_Value(&varTableName);
    
            // Display the record.
            if (varTableName.vt == VT_BSTR)
            {
                wprintf(L"%s\n", V_BSTR(&varTableName));
            }
    
            // Move to the next record.
            hr = spRecordset->MoveNext();
            bEOF = VARIANT_TRUE;
            hr = spRecordset->get_EOF(&bEOF);
        }
    
        // Release smart pointers.
        spRecordset = NULL;
        spConnection = NULL;
    
        CoUninitialize();
        return 0;
    }
    

    You can extract the IDL of ADODB by using OleView (on my PC it was installed with Visual Studio under C:\Program Files\Microsoft SDKs\Windows\v7.0\Bin\OleView.exe). Once you have OleView running:

    • File > View TypeLib ...
    • Navigate to C:\Program Files\Common Files\System\ADO\msado15.dll
    • Click Open

    This will give you a full syntax of the ADODB library which will help you use it in C++. You can also refer to Microsoft MSDN's ADO API Reference.

提交回复
热议问题