Microsoft Knowledge Base Email Alertz

The Jet OLE DB provider supports the

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 248106 - Last Review: August 30, 2004 - Revision: 2.2

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

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:
http://msdn.microsoft.com/dataaccess (http://msdn.microsoft.com/dataaccess)
If you are using MDAC 2.1, see the following article for information on how to obtain this header file:
228525  (http://kbalertz.com/Feedback.aspx?kbNumber=228525/EN-US/ ) 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.

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
Keywords: 
kbhowto kbjet KB248106
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate