How do you create a database with the Jet 4.0 engine? 
Author Message
 How do you create a database with the Jet 4.0 engine?

We have used the program below to create a database using the Jet 3.51
engine, but when we try to use the Jet 4.0 engine it fails.  Does anyone
know how to get it to work?

/**************************************************************
* OLE DB
**************************************************************/
#define UNICODE
#define _UNICODE
#define DBINITCONSTANTS
#define INITGUID

#include <windows.h>
#include <stdio.h>

#include <oledb.h>
#include <oledberr.h>
#include <msjetoledb.h>

#include <ks.h>
//#include <tchar.h>
//#include <stddef.h>
//#include <iostream.h>

//Prototypes
HRESULT  myInitDSO(IDBInitialize** ppIDBI);
HRESULT  mySetInitProps(IDBInitialize* pIDBInitialize);
HRESULT  myCreateDataStore(IDBInitialize* pIDBInitialize);
HRESULT  myCreateTables(IDBInitialize** ppIDBInitialize);
HRESULT  myCreateIndexes(IDBInitialize** ppIDBInitialize);

int main()
{
   IDBInitialize* pIDBInitialize = NULL;
   IRowset*       pIRowset = NULL;

   //Init Ole and set up the DLLs
   CoInitialize(NULL);

   //Connect to the data source
   if (FAILED(myInitDSO(&pIDBInitialize)))
      goto EXIT;

   //Create tables
   if (FAILED(myCreateTables(&pIDBInitialize)))
      return (E_FAIL);

   //Create indexes
   if (FAILED(myCreateIndexes(&pIDBInitialize)))
      return (E_FAIL);

   EXIT:

   //clean up and disconnect
   if (pIRowset != NULL)
      pIRowset->Release();
   if (pIDBInitialize != NULL)
      pIDBInitialize->Release();

   CoUninitialize();

   return (0);

Quote:
}      

/**************************************************************
* Initialize the data source
**************************************************************/
HRESULT myInitDSO (IDBInitialize** ppIDBInitialize)
{
   HRESULT  hr;

   //Create an instance of the Jet provider
   hr = CoCreateInstance(CLSID_JETOLEDB_4_00,NULL,CLSCTX_INPROC_SERVER,
                         IID_IDBInitialize, (void**)ppIDBInitialize);
   if (*ppIDBInitialize == NULL)
      return (E_FAIL);

   if (FAILED(mySetInitProps(*ppIDBInitialize)))
      return (E_FAIL);

   if (FAILED(myCreateDataStore(*ppIDBInitialize)))
      return (E_FAIL);

   return (NOERROR);    

Quote:
}

/**************************************************************
* Set initialization properties
**************************************************************/
HRESULT mySetInitProps (IDBInitialize* pIDBInitialize)
{
   const ULONG    nProps = 5;
   IDBProperties* pIDBProperties;
   DBPROP         InitProperties[nProps];
   DBPROPSET      rgInitPropSet;
   HRESULT        hr;

   // initialize common properties options  
   for (ULONG i = 0; i < nProps; i++)
   {
      VariantInit(&InitProperties[i].vValue);
      InitProperties[i].dwOptions = DBPROPOPTIONS_REQUIRED;
      InitProperties[i].colid = DB_NULLID;
   }

   //Password
   InitProperties[0].dwPropertyID = DBPROP_AUTH_PASSWORD;
   InitProperties[0].vValue.vt = VT_BSTR;
   InitProperties[0].vValue.bstrVal = SysAllocString(OLESTR(""));    

   //User ID
   InitProperties[1].dwPropertyID = DBPROP_AUTH_USERID;
   InitProperties[1].vValue.vt = VT_BSTR;
   InitProperties[1].vValue.bstrVal = SysAllocString(OLESTR("Admin"));

   //Data source
   InitProperties[2].dwPropertyID = DBPROP_INIT_DATASOURCE;
   InitProperties[2].vValue.vt = VT_BSTR;
   InitProperties[2].vValue.bstrVal =
SysAllocString(OLESTR("e:\\access\\createdb.mdb"));    

   //Locale ID
   InitProperties[3].dwPropertyID = DBPROP_INIT_LCID;
   InitProperties[3].vValue.vt = VT_I4;
   InitProperties[3].vValue.iVal = (long)1033;

   //Provider specific
   InitProperties[4].dwPropertyID = DBPROP_INIT_PROVIDERSTRING;
   InitProperties[4].vValue.vt = VT_BSTR;
   InitProperties[4].vValue.bstrVal = SysAllocString(OLESTR(""));
   //InitProperties[4].vValue.bstrVal =
SysAllocString(OLESTR(";COUNTRY=0;CP=1252;LANGID=0x0409"));

   rgInitPropSet.guidPropertySet = DBPROPSET_DBINIT;
   rgInitPropSet.cProperties = nProps;
   rgInitPropSet.rgProperties = InitProperties;

   pIDBInitialize->QueryInterface(IID_IDBProperties,
                                  (void**) &pIDBProperties);
   hr = pIDBProperties->SetProperties(1, &rgInitPropSet);

   SysFreeString(InitProperties[0].vValue.bstrVal);
   SysFreeString(InitProperties[1].vValue.bstrVal);
   SysFreeString(InitProperties[2].vValue.bstrVal);
   SysFreeString(InitProperties[4].vValue.bstrVal);

   pIDBProperties->Release();

   return (hr);    

Quote:
}

/**************************************************************
* create data store
**************************************************************/
HRESULT myCreateDataStore (IDBInitialize* pIDBInitialize)
{
   IDBDataSourceAdmin*  pIDBDataSourceAdmin;
   DBPROPSET            rgInitPropSet;
   HRESULT              hr;

   hr = pIDBInitialize->QueryInterface(IID_IDBDataSourceAdmin,
                                (void**)&pIDBDataSourceAdmin);
   if (hr == S_OK)
      hr =
pIDBDataSourceAdmin->CreateDataSource(0,&rgInitPropSet,NULL,IID_IDBDataS
ourceAdmin,NULL);

   return (hr);    

Quote:
}

/**************************************************************
* create tables
**************************************************************/
HRESULT myCreateTables (IDBInitialize** ppIDBInitialize)
{
   IDBCreateSession* pIDBCreateSession;
   ITableDefinition* pITableDefinition;
   IDBCreateCommand* pIDBCreateCommand;
   ICommandText*     pICommandText;

   HRESULT           hr;
   DBID              cTableID;
   DBCOLUMNDESC      cColDescs[2];
   DBID*             pNewTableID = NULL;
   IRowset*          pRowset = NULL;  

   LPCTSTR           wCmdString =
                     OLESTR("Create Table Table1(Column1 INTEGER,
Column2 TEXT(20))");                                

   //Get session object
   if (FAILED((*ppIDBInitialize)->QueryInterface(IID_IDBCreateSession,

(void**)&pIDBCreateSession)))
      return (E_FAIL);

   //Create command object
   hr = pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand,

(IUnknown**)&pIDBCreateCommand);

   hr = pIDBCreateSession->Release();

   //Access command text interface
   hr = pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText,
                                         (IUnknown**)&pICommandText);

   hr = pIDBCreateCommand->Release();

   //Specify command text
   hr = pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString);

   //Execute the command
   hr = pICommandText->Execute(NULL, IID_NULL, NULL, NULL, NULL);

   hr = pIDBCreateCommand->Release();
   return (NOERROR);    

Quote:
}

/**************************************************************
* create tables
**************************************************************/
HRESULT myCreateIndexes (IDBInitialize** ppIDBInitialize)
{
   IDBCreateSession* pIDBCreateSession;
   ITableDefinition* pITableDefinition;
   IDBCreateCommand* pIDBCreateCommand;
   ICommandText*     pICommandText;

   HRESULT           hr;
   DBID              cTableID;
   DBCOLUMNDESC      cColDescs[2];
   DBID*             pNewTableID = NULL;
   IRowset*          pRowset = NULL;  

   LPCTSTR           wCmdString =
                     OLESTR("Create Index Index1 on Table1(Column1) with
primary");                                

   //Get session object
   if (FAILED((*ppIDBInitialize)->QueryInterface(IID_IDBCreateSession,

(void**)&pIDBCreateSession)))
      return (E_FAIL);

   //Create command object
   hr = pIDBCreateSession->CreateSession(NULL, IID_IDBCreateCommand,

(IUnknown**)&pIDBCreateCommand);

   hr = pIDBCreateSession->Release();

   //Access command text interface
   hr = pIDBCreateCommand->CreateCommand(NULL, IID_ICommandText,
                                         (IUnknown**)&pICommandText);

   hr = pIDBCreateCommand->Release();

   //Specify command text
   hr = pICommandText->SetCommandText(DBGUID_DBSQL, wCmdString);

   //Execute the command
   hr = pICommandText->Execute(NULL, IID_NULL, NULL, NULL, NULL);

   hr = pIDBCreateCommand->Release();

   return (NOERROR);    

Quote:
}



Mon, 04 Mar 2002 03:00:00 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. How can I create a new database using OLEDB provider for Microsof t Jet Engine 4.0

2. Jet Engine Database access

3. Security and Access Control for Jet Engine Database from C++

4. Jet Database Engine Init Error

5. Unable to initialise Jet DAO Database engine

6. Jet database engine....

7. How fast is MS Jet Database Engine?

8. The Microsoft Jet database engine cannot open the file

9. Creating a new database with DAO (Other than Microsoft Jet Database)

10. Help- DAO database conversion from Microsoft Jet 3.0 to 4.0

11. Using Jet 4.0 Unicode databases with DAO

12. how to programatically set up Jet Engine Security

 

 
Powered by phpBB® Forum Software