Establishing a Connection to a Data Source

OLE DB and SQL Server

OLE DB and SQL Server

Establishing a Connection to a Data Source

To access the Microsoft OLE DB Provider for SQL Server (SQLOLEDB), 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 SQLOLEDB, the class identifier is CLSID_SQLOLEDB.

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 Microsoft® SQL Server™ running on the computer, the server name is specified as ServerName\\InstanceName (the escape sequence \\ is used for the backslash).

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_SQLOLEDB, 
                 NULL, 
                 CLSCTX_INPROC_SERVER,
                 IID_IDBInitialize, 
                 (void **) &pIDBInitialize)

This call to CoCreateInstance creates a single object of the class associated with CLSID_SQLOLEDB (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.

This is a sample function that initializes and establishes a connection to the data source:

void InitializeAndEstablishConnection()
{    
    //Initialize the COM library.
    CoInitialize(NULL);
    //Obtain access to the SQLOLEDB provider.
    hr = CoCreateInstance(CLSID_SQLOLEDB, 
                          NULL, 
                          CLSCTX_INPROC_SERVER,
                          IID_IDBInitialize, 
                          (void **) &pIDBInitialize);
    /*
    Initialize the property values needed 
    to establish the connection.
    */
    for(i = 0; i < 4; i++) 
        VariantInit(&InitProperties[i].vValue);
    //Server name.
    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_USERID; 
    InitProperties[2].vValue.vt     = VT_BSTR;
    InitProperties[2].vValue.bstrVal= SysAllocString(L"sa");
    InitProperties[2].dwOptions     = DBPROPOPTIONS_REQUIRED;
    InitProperties[2].colid         = DB_NULLID;
    //Password.
    InitProperties[3].dwPropertyID  = DBPROP_AUTH_PASSWORD;
    InitProperties[3].vValue.vt     = VT_BSTR;
    InitProperties[3].vValue.bstrVal= SysAllocString(L"");
    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()
}