Sdílet prostřednictvím


Establishing a Connection to a Data Source

To access the SQL Server Native Client OLE DB provider, the consumer must first create an instance of a data source object by calling the CoCreateInstance method. A unique class identifier (CLSID) identifies each OLE DB provider. For the SQL Server Native Client OLE DB provider, the class identifier is CLSID_SQLNCLI10. You can also use the symbol SQLNCLI_CLSID that will resolve to the SQL Server Native Client OLE DB provider that is used in the sqlncli.h that you reference. 

The data source object exposes the IDBProperties interface, which the consumer uses to provide basic authentication information such as server name, database name, user ID, and password. The IDBProperties::SetProperties method is called to set these properties.

If there are multiple instances of SQL Server running on the computer, the server name is specified as ServerName\InstanceName.

The data source object also exposes the IDBInitialize interface. After the properties are set, connection to the data source is established by calling the IDBInitialize::Initialize method. For example:

CoCreateInstance(CLSID_SQLNCLI10, 
                 NULL, 
                 CLSCTX_INPROC_SERVER,
                 IID_IDBInitialize, 
                 (void **) &pIDBInitialize)

This call to CoCreateInstance creates a single object of the class associated with CLSID_SQLNCLI10 (CSLID associated with the data and code that will be used to create the object). IID_IDBInitialize is a reference to the identifier of the interface (IDBInitialize) to be used to communicate with the object.

The following is a sample function that initializes and establishes a connection to the data source. For information about acessing OLE DB sample applications, see Considerations for Installing SQL Server Samples and Sample Databases.

void InitializeAndEstablishConnection() {
   // Initialize the COM library.
   CoInitialize(NULL);

   // Obtain access to the SQL Server Native Client OLE DB provider.
   hr = CoCreateInstance(CLSID_SQLNCLI10, 
                         NULL, 
                         CLSCTX_INPROC_SERVER,
                         IID_IDBInitialize, 
                         (void **) &pIDBInitialize);
   // Initialize property values needed to establish connection.
   for (i = 0 ; i < 4 ; 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;
   InitProperties[3].dwOptions    = DBPROPOPTIONS_REQUIRED;
   InitProperties[3].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   = 4;
   rgInitPropSet[0].rgProperties   = InitProperties;

   // Set initialization properties.
   hr = pIDBInitialize->QueryInterface(IID_IDBProperties, 
                           (void **)&pIDBProperties);
   hr = pIDBProperties->SetProperties(1, rgInitPropSet); 
   pIDBProperties->Release();

   // Now establish the connection to the data source.
   pIDBInitialize->Initialize();
}