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
Om toegang te krijgen tot de OLE DB Driver voor SQL Server, moet de consument eerst een instantie van een databronobject aanmaken door de methode aan te CoCreateInstance roepen. Een unieke klasse-identificatie (CLSID) identificeert elke OLE DB-provider. Voor de OLE DB-driver voor SQL Server kun je het MSOLEDBSQL_CLSID-symbool gebruiken dat in het msoledbsql.h bestand is gedefinieerd.
Het databronobject stelt de IDBProperties interface beschikbaar, die de consument gebruikt om basisauthenticatie-informatie te verstrekken zoals servernaam, databasenaam, gebruikers-ID en wachtwoord. De IDBProperties::SetProperties methode wordt aangeroepen om deze eigenschappen in te stellen.
Als er meerdere instanties van SQL Server op de computer draaien, wordt de servernaam gespecificeerd als ServerName\InstanceName.
Het databronobject maakt ook de IDBInitialize interface bloot. Nadat de eigenschappen zijn ingesteld, wordt de verbinding met de databron tot stand gebracht door de methode aan te IDBInitialize::Initialize roepen. Voorbeeld:
CoCreateInstance(MSOLEDBSQL_CLSID,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void **) &pIDBInitialize)
Deze aanroep om CoCreateInstance een enkel object te creƫren van de klasse die aan MSOLEDBSQL_CLSID is gekoppeld (CSLID gekoppeld aan de data en code die gebruikt zullen worden om het object te maken). IID_IDBInitialize is een verwijzing naar de identificatie van de interface (IDBInitialize) die gebruikt moet worden om met het object te communiceren.
Het volgende voorbeeld laat zien hoe je initialiseert en een verbinding kunt maken met de databron.
#include "msoledbsql.h"
#include <stdio.h>
HRESULT InitializeAndEstablishConnection(IDBInitialize *&pIDBInitialize);
void main() {
IDBInitialize *pIDBInitialize = nullptr;
HRESULT hr = S_OK;
// Initialize The Component Object Module Library
CoInitialize(nullptr);
hr = InitializeAndEstablishConnection(pIDBInitialize);
if (FAILED(hr)) {
printf("Failed to establish connection.\r\n");
goto _ExitMain;
}
// Insert code that uses the established connection
_ExitMain:
// Free Up All Allocated Memory
if (pIDBInitialize)
{
pIDBInitialize->Uninitialize();
pIDBInitialize->Release();
pIDBInitialize = nullptr;
}
// Release The Component Object Module Library
CoUninitialize();
}
HRESULT InitializeAndEstablishConnection(IDBInitialize *&pIDBInitialize) {
IDBProperties *pIDBProperties = nullptr;
DBPROP InitProperties[3] = { 0 };
DBPROPSET rgInitPropSet[1] = { 0 };
HRESULT hr = S_OK;
// Obtain access to the OLE DB Driver for SQL Server.
hr = CoCreateInstance(MSOLEDBSQL_CLSID,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void **)&pIDBInitialize);
if (FAILED(hr)) {
printf("Failed to obtain access to the OLE DB Driver.\r\n");
goto _ExitInitialize;
}
// Initialize property values needed to establish connection.
for (int i = 0; i < 3; i++) {
VariantInit(&InitProperties[i].vValue);
}
// Server name.
// See DBPROP structure for more information on InitProperties
InitProperties[0].dwPropertyID = DBPROP_INIT_DATASOURCE;
InitProperties[0].vValue.vt = VT_BSTR;
InitProperties[0].vValue.bstrVal = SysAllocString(L"Server");
InitProperties[0].dwOptions = DBPROPOPTIONS_REQUIRED;
InitProperties[0].colid = DB_NULLID;
// Database.
InitProperties[1].dwPropertyID = DBPROP_INIT_CATALOG;
InitProperties[1].vValue.vt = VT_BSTR;
InitProperties[1].vValue.bstrVal = SysAllocString(L"database");
InitProperties[1].dwOptions = DBPROPOPTIONS_REQUIRED;
InitProperties[1].colid = DB_NULLID;
// Username (login).
InitProperties[2].dwPropertyID = DBPROP_AUTH_INTEGRATED;
InitProperties[2].vValue.vt = VT_BSTR;
InitProperties[2].vValue.bstrVal = SysAllocString(L"SSPI");
InitProperties[2].dwOptions = DBPROPOPTIONS_REQUIRED;
InitProperties[2].colid = DB_NULLID;
// Construct the DBPROPSET structure(rgInitPropSet). The
// DBPROPSET structure is used to pass an array of DBPROP
// structures (InitProperties) to the SetProperties method.
rgInitPropSet[0].guidPropertySet = DBPROPSET_DBINIT;
rgInitPropSet[0].cProperties = 3;
rgInitPropSet[0].rgProperties = InitProperties;
// Set initialization properties.
hr = pIDBInitialize->QueryInterface(IID_IDBProperties,
(void **)&pIDBProperties);
if (FAILED(hr)) {
printf("Failed to obtain an IDBProperties interface.\r\n");
goto _ExitInitialize;
}
hr = pIDBProperties->SetProperties(1, rgInitPropSet);
if (FAILED(hr)) {
printf("Failed to set initialization properties.\r\n");
goto _ExitInitialize;
}
// Now establish the connection to the data source.
hr = pIDBInitialize->Initialize();
if (FAILED(hr)) {
printf("Failed to establish connection with the server.\r\n");
goto _ExitInitialize;
}
_ExitInitialize:
if (pIDBProperties)
{
pIDBProperties->Release();
pIDBProperties = nullptr;
}
if (FAILED(hr))
{
if (pIDBInitialize)
{
pIDBInitialize->Release();
pIDBInitialize = nullptr;
}
}
return hr;
}