Microsoft KB Archive/248106

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 13:50, 21 July 2020 by X010 (talk | contribs) (Text replacement - """ to """)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


How To Use the IDBDataSourceAdmin Interface of the Jet OLE DB Provider to Create a Blank .mdb File

Article ID: 248106

Article Last Modified on 8/30/2004



APPLIES TO

  • Microsoft OLE DB Provider for Jet 4.0
  • Microsoft OLE DB 2.1
  • Microsoft OLE DB 2.5
  • Microsoft OLE DB 2.6



This article was previously published under Q248106

SUMMARY

The Jet OLE DB provider supports the IDBDataSourceAdmin interface, an optional OLE DB interface that is used to create, destroy, and modify data source objects. This article demonstrates how to use this interface to create an empty Microsoft Access .mdb file.

MORE INFORMATION

It is important to distinguish between the data source object and the data store. The data source object is the object that is used by the OLE DB consumer code. The data store is the actual physical source of data, such as a server database.

The IDBDataSourceAdmin::CreateDataSource function creates and initializes a data source object and the data store in one atomic operation. The OLE DB consumer must set any Data Source Creation or Initialization properties that are required by the provider to create and initialize the data source.

NOTE: If you are using Microsoft Data Access Components (MDAC) version 2.5, you may need to install the MDAC Software Development Kit (SDK) in order to obtain the Msjetoledb.h file. You can download the SDK from the following Microsoft Web site:

If you are using MDAC 2.1, see the following article for information on how to obtain this header file:

228525 PATCH: JetVC.exe VC++ Support Files for the Jet OLE DB Provider


The following code demonstrates how to create an empty Access database in the location C:\Createdb.mdb:

/************************************************************** 
* Create a blank .mdb file with the Jet 4.0 provider.
**************************************************************/ 
#define UNICODE
#define _UNICODE
#define DBINITCONSTANTS
#define INITGUID

#include <windows.h>
#include <msjetoledb.h> //MDAC SDK
#include <oledb.h>

struct initCom
{
    initCom() { CoInitialize(NULL); }
    ~initCom() { CoUninitialize(); }
} _inst_initCom;


int main()
{
    IDBInitialize* pIDBInitialize = NULL;
    IDBDataSourceAdmin*  pIDBDataSourceAdmin = NULL;
    HRESULT  hr;
    const ULONG          nDBProps = 1;
    const ULONG          nPropsets = 1;
    DBPROP         DBProperties[nDBProps];
    DBPROPSET      DBPropSet;
    DBPROPSET      propSetArray[nPropsets];
    
    DBProperties[0].dwPropertyID = DBPROP_INIT_DATASOURCE; 

    DBProperties[0].vValue.vt = VT_BSTR;
    DBProperties[0].dwOptions = DBPROPOPTIONS_OPTIONAL;
    DBProperties[0].colid = DB_NULLID;
    DBProperties[0].vValue.bstrVal = 
        SysAllocString(OLESTR("C:\Createdb.mdb"));    
    
    DBPropSet.guidPropertySet = DBPROPSET_DBINIT;
    DBPropSet.cProperties = nDBProps;
    DBPropSet.rgProperties = DBProperties;
    
    propSetArray[0] = DBPropSet;
    
    hr = CoCreateInstance(CLSID_JETOLEDB_4_00,NULL,
        CLSCTX_INPROC_SERVER,IID_IDBInitialize, 
        (void**) & pIDBInitialize);
    
    if (FAILED(hr))
        return (E_FAIL);
    
    hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin, 
        (void**)&pIDBDataSourceAdmin);
    
    if (SUCCEEDED(hr))
        hr = pIDBDataSourceAdmin->CreateDataSource(nPropsets,
        propSetArray,NULL,IID_IDBDataSourceAdmin,NULL);
    
    pIDBDataSourceAdmin->Release();
    
    SysFreeString(DBProperties[0].vValue.bstrVal);
    
    if (pIDBInitialize != NULL)
        pIDBInitialize->Release();
    
    return (0);
}    
                

NOTE: You can modify the version format of the Access .mdb file by setting the DBPROP_JETOLEDB_ENGINE property of the DBPROPSET_JETOLEDB_DBINIT propset. If this property is not specified, the .mdb file is created in the Access 2000/Jet 4.0 format.

Keywords: kbhowto kbjet KB248106