Megosztás a következőn keresztül:


Előadások

Vonatkozik a következőkre:SQL ServerAzure SQL DatabaseAzure SQL Managed InstanceAzure Synapse AnalyticsAnalitikai Platform System (PDW)SQL adatbázis a Microsoft Fabric-ben

OLE DB-illesztő letöltése

Az OLE DB Driver for SQL Server session egyetlen kapcsolatot képvisel egy SQL Server példányához.

Az SQL Server OLE DB Driver megköveteli, hogy a munkamenetek korlátozzák az adatforrás tranzakciós terét. Minden parancsobjektum, amely egy adott session objektumból jött létre, részt vesz a session objektum helyi vagy elosztott tranzakciójában.

Az első alkalomobjektum, amely az inicializált adatforráson jött létre, kapja az inicializációkor létrehozott SQL Server kapcsolatot. Amikor az session objektum interfészein minden hivatkozás elérhetővé válik, az SQL Server példányához való csatlakozás elérhetővé válik egy másik, az adatforráson létrehozott session objektum számára.

Az adatforráson létrehozott további session objektum saját kapcsolatot teremt az SQL Server példányával, ahogy azt az adatforrás megadja. Az SQL Server példányához való kapcsolat akkor szakad meg, amikor az alkalmazás minden hivatkozást kiszabadít az adott ülés által létrehozott objektumokra.

Az alábbi példa bemutatja, hogyan lehet az OLE DB Driver for SQL Server segítségével csatlakozni egy SQL Server adatbázishoz:

int main()  
{  
    // Interfaces used in the example.  
    IDBInitialize*      pIDBInitialize      = NULL;  
    IDBCreateSession*   pIDBCreateSession   = NULL;  
    IDBCreateCommand*   pICreateCmd1        = NULL;  
    IDBCreateCommand*   pICreateCmd2        = NULL;  
    IDBCreateCommand*   pICreateCmd3        = NULL;  
  
    // Initialize COM.  
    if (FAILED(CoInitialize(NULL)))  
    {  
        // Display error from CoInitialize.  
        return (-1);  
    }  
  
    // Get the memory allocator for this task.  
    if (FAILED(CoGetMalloc(MEMCTX_TASK, &g_pIMalloc)))  
    {  
        // Display error from CoGetMalloc.  
        goto EXIT;  
    }  
  
    // Create an instance of the data source object.  
    if (FAILED(CoCreateInstance(CLSID_MSOLEDBSQL, NULL,  
        CLSCTX_INPROC_SERVER, IID_IDBInitialize, (void**)  
        &pIDBInitialize)))  
    {  
        // Display error from CoCreateInstance.  
        goto EXIT;  
    }  
  
    // The InitFromPersistedDS function   
    // performs IDBInitialize->Initialize() establishing  
    // the first application connection to the instance of SQL Server.  
    if (FAILED(InitFromPersistedDS(pIDBInitialize, L"MyDataSource",  
        NULL, NULL)))  
    {  
        goto EXIT;  
    }  
  
    // The IDBCreateSession interface is implemented on the data source  
    // object. Maintaining the reference received maintains the   
    // connection of the data source to the instance of SQL Server.  
    if (FAILED(pIDBInitialize->QueryInterface(IID_IDBCreateSession,  
        (void**) &pIDBCreateSession)))  
    {  
        // Display error from pIDBInitialize.  
        goto EXIT;  
    }  
  
    // Releasing this has no effect on the SQL Server connection  
    // of the data source object because of the reference maintained by  
    // pIDBCreateSession.  
    pIDBInitialize->Release();  
    pIDBInitialize = NULL;  
  
    // The session created next receives the SQL Server connection of  
    // the data source object. No new connection is established.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd1)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
    // A new connection to the instance of SQL Server is established to support the  
    // next session object created. On successful completion, the  
    // application has two active connections on the SQL Server.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd2)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
    // pICreateCmd1 has the data source connection. Because the  
    // reference on the IDBCreateSession interface of the data source  
    // has not been released, releasing the reference on the session  
    // object does not terminate a connection to the instance of SQL Server.  
    // However, the connection of the data source object is now   
    // available to another session object. After a successful call to   
    // Release, the application still has two active connections to the   
    // instance of SQL Server.  
    pICreateCmd1->Release();  
    pICreateCmd1 = NULL;  
  
    // The next session created gets the SQL Server connection  
    // of the data source object. The application has two active  
    // connections to the instance of SQL Server.  
    if (FAILED(pIDBCreateSession->CreateSession(NULL,  
        IID_IDBCreateCommand, (IUnknown**) &pICreateCmd3)))  
    {  
        // Display error from pIDBCreateSession.  
        goto EXIT;  
    }  
  
EXIT:  
    // Even on error, this does not terminate a SQL Server connection   
    // because pICreateCmd1 has the connection of the data source   
    // object.  
    if (pICreateCmd1 != NULL)  
        pICreateCmd1->Release();  
  
    // Releasing the reference on pICreateCmd2 terminates the SQL  
    // Server connection supporting the session object. The application  
    // now has only a single active connection on the instance of SQL Server.  
    if (pICreateCmd2 != NULL)  
        pICreateCmd2->Release();  
  
    // Even on error, this does not terminate a SQL Server connection   
    // because pICreateCmd3 has the connection of the   
    // data source object.  
    if (pICreateCmd3 != NULL)  
        pICreateCmd3->Release();  
  
    // On release of the last reference on a data source interface, the  
    // connection of the data source object to the instance of SQL Server is broken.  
    // The example application now has no SQL Server connections active.  
    if (pIDBCreateSession != NULL)  
        pIDBCreateSession->Release();  
  
    // Called only if an error occurred while attempting to get a   
    // reference on the IDBCreateSession interface of the data source.  
    // If so, the call to IDBInitialize::Uninitialize terminates the   
    // connection of the data source object to the instance of SQL Server.  
    if (pIDBInitialize != NULL)  
    {  
        if (FAILED(pIDBInitialize->Uninitialize()))  
        {  
            // Uninitialize is not required, but it fails if an  
            // interface has not been released. Use it for  
            // debugging.  
        }  
        pIDBInitialize->Release();  
    }  
  
    if (g_pIMalloc != NULL)  
        g_pIMalloc->Release();  
  
    CoUninitialize();  
  
    return (0);  
}  

Az OLE DB Driver SQL Server session objektumokhoz való csatlakoztatása jelentős terhelést generálhat azoknak az alkalmazásoknak, amelyek folyamatosan létrehoznak és adnak ki. A többletterhelést minimalizálhatjuk, ha hatékonyan kezeljük az SQL Server session objektumok OLE DB Driverét. Az OLE DB Driver SQL Server alkalmazásokhoz képes aktív maradni egy session objektum SQL Server kapcsolatát azzal, hogy legalább az objektum egyik interfészén tart egy hivatkozást.

Például, ha egy parancsalkotó objektumhivatkozások készletét fenntartjuk, aktív kapcsolatokat tart fenn az adott munkaalek-objektumok számára a poolban. Mivel a session objektumokra van szükség, a pool karbantartó kód érvényes IDBCreateCommand interfész mutatót továbbít az alkalmazásmódszerhez, amely megköveteli a szekciót. Amikor az alkalmazás módszer már nem igényli az ülést, a metódus visszaadja az interfész mutatót a pool karbantartási kódhoz, ahelyett, hogy az alkalmazás hivatkozását a parancsalkotó objektumra engedné fel.

Megjegyzés:

Az előző példában az IDBCreateCommand interfészt azért használják, mert az ICommand interfésze a GetDBSession metódusot valósítja meg, amely az egyetlen módszer a parancs- vagy sorhalmazi körben, amely lehetővé teszi, hogy egy objektum meghatározza, melyik ülésszakon létrehozták. Ezért egy parancsobjektum, és csak egy parancsobjektum, lehetővé teszi az alkalmazás számára, hogy egy adatforrás objektummutatót szerezzen, amelyből további üléseket lehet létrehozni.

Lásd még:

Adatforrás objektumok (OLE DB)