Notitie
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen u aan te melden of de directory te wijzigen.
Voor toegang tot deze pagina is autorisatie vereist. U kunt proberen de mappen te wijzigen.
Van toepassing op:SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform Systeem (PDW)
SQL-database in Microsoft Fabric
OLE DB-stuurprogramma downloaden
Een OLE DB Driver voor SQL Server-sessie vertegenwoordigt een enkele verbinding met een instantie van SQL Server.
De OLE DB Driver voor SQL Server vereist dat sessies de transactieruimte voor een databron afbakenen. Alle commandoobjecten die zijn gemaakt van een specifiek sessieobject nemen deel aan de lokale of gedistribueerde transactie van het sessieobject.
Het eerste sessieobject dat op de geïnitialiseerde databron wordt aangemaakt, ontvangt de SQL Server-verbinding die bij initialisatie is opgezocht. Wanneer alle referenties op de interfaces van het sessieobject worden vrijgegeven, wordt de verbinding met de instantie van SQL Server beschikbaar voor een ander sessieobject dat op de databron is gemaakt.
Een extra sessieobject dat op de databron wordt aangemaakt, legt zijn eigen verbinding tot stand met de instantie van SQL Server zoals gespecificeerd door de databron. De verbinding met de instantie van SQL Server wordt verbroken wanneer de applicatie alle referenties naar objecten die die sessie zijn aangemaakt, vrijgeeft.
Het volgende voorbeeld laat zien hoe je de OLE DB Driver voor SQL Server kunt gebruiken om verbinding te maken met een SQL Server-database:
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);
}
Het koppelen van OLE DB Driver voor SQL Server-sessieobjecten aan een instantie van SQL Server kan aanzienlijke overhead genereren voor applicaties die continu sessieobjecten aanmaken en vrijgeven. De overhead kan worden geminimaliseerd door OLE DB Driver voor SQL Server-sessieobjecten efficiënt te beheren. OLE DB Driver voor SQL Server-applicaties kan de SQL Server-verbinding van een sessieobject actief houden door een referentie te behouden op ten minste één interface van het object.
Bijvoorbeeld, het onderhouden van een pool van commandocreatieobjectreferenties houdt actieve verbindingen voor die sessieobjecten in de pool. Omdat sessieobjecten nodig zijn, geeft de poolonderhoudscode een geldige IDBCreateCommand-interfacepointer door naar de applicatiemethode die de sessie vereist. Wanneer de applicatiemethode de sessie niet langer vereist, stuurt de methode de interfacepointer terug naar de poolonderhoudscode in plaats van de applicatiereferentie naar het commandocreatieobject vrij te geven.
Opmerking
In het voorgaande voorbeeld wordt de IDBCreateCommand-interface gebruikt omdat de ICommand-interface de GetDBSesson-methode implementeert, de enige methode binnen commando- of rowset-scope die een object toestaat de sessie waarop het is aangemaakt te bepalen. Daarom stelt een commandoobject, en alleen een commandoobject, een applicatie in staat een databronobjectpointer op te halen waaruit extra sessies kunnen worden aangemaakt.