建立資料來源的連接
適用於:SQL Server Azure SQL 資料 Azure SQL 受控執行個體 Azure Synapse Analytics Analytics Platform System (PDW)
若要存取 OLE DB Driver for SQL Server,取用者必須先呼叫 CoCreateInstance
方法來建立資料來源物件的執行個體。 唯一類別識別項 (CLSID) 會識別每個 OLE DB 提供者。 針對 OLE DB Driver for SQL Server,您可以使用 檔案中 msoledbsql.h
定義的MSOLEDBSQL_CLSID符號。
資料來源物件會公開 IDBProperties
介面,取用者可以使用這個介面來提供基本驗證資訊;例如,伺服器名稱、資料庫名稱、使用者識別碼和密碼。 您可以呼叫 IDBProperties::SetProperties
方法以設定這些屬性。
如果有多個 SQL Server 執行個體在電腦上執行,伺服器名稱會指定為 ServerName\InstanceName。
資料來源物件也會公開 IDBInitialize
介面。 屬性設定之後,就會呼叫 IDBInitialize::Initialize
方法來建立資料來源的連線。 例如:
CoCreateInstance(MSOLEDBSQL_CLSID,
NULL,
CLSCTX_INPROC_SERVER,
IID_IDBInitialize,
(void **) &pIDBInitialize)
這個呼叫會 CoCreateInstance
建立與MSOLEDBSQL_CLSID相關聯之類別的單一物件(與將用來建立對象的數據和程式代碼相關聯的 CSLID)。 IID_IDBInitialize 是介面 (IDBInitialize
) 識別項的參考,用於與物件進行通訊。
下列範例示範如何初始化並建立與資料來源的連線。
#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;
}