I have code that allows me to create a new *.mdb along with new tables under DAO. But can it be done under OLE DB or ADO?
If so, do you have a web site, book, article, etc reference that'll get me started?
Thanks..........Paul
Printable View
I have code that allows me to create a new *.mdb along with new tables under DAO. But can it be done under OLE DB or ADO?
If so, do you have a web site, book, article, etc reference that'll get me started?
Thanks..........Paul
I have found a good book... this name: ADO 2.0 Programmer's Reference
These examples is in VB but it`s good Book. He explain All about ADO
Thanks for the tip.
But I guess I have to go back to the original question: Can I create an *.mdb under msvc++ on the fly? If so, I'll buy the book.
Paul
yes you can I do it
With Ole DB the best way to do it is through SQL commands. It can be done through the Ole DB interfaces but this is _not_ programmer friendly. The following code uses the template classes for Ole DB which are in VC++ 6...
// Create provider instance and get IDataSourceAdmin interface.
CComPtr<IDBDataSourceAdmin> spAdmin;
hr = ::CoCreateInstance(CLSID_JETOLEDB_4_00, NULL, CLSCTX_INPROC_SERVER,
IID_IDBDataSourceAdmin, (void**)&spAdmin);
if (FAILED(hr))
return hr;
//
// Standard initialisation properties
CDBPropSet initStd(DBPROPSET_DBINIT);
initStd.AddProperty(DBPROP_AUTH_PASSWORD, ""); // password
initStd.AddProperty(DBPROP_AUTH_USERID, _T("Admin")); // username
initStd.AddProperty(DBPROP_INIT_DATASOURCE, lpszSrcPath); // file name of data source
initStd.AddProperty(DBPROP_INIT_MODE, (long)DB_MODE_SHARE_DENY_NONE); // access permissions
initStd.AddProperty(DBPROP_INIT_PROMPT, (short)DBPROMPT_NOPROMPT); // user not prompted for init info
//
// Jet 4.00 specific initialisation properties
CDBPropSet initJet(DBPROPSET_JETOLEDB_DBINIT);
initJet.AddProperty(DBPROP_JETOLEDB_ENGINE, (long)JETDBENGINETYPE_JET3X); // Create database as Access 97 compatible
//
CDBPropSet dbinit[2] = { initStd, initJet };
//
// Note: CreateDataSource will also initialise the data source (via
// Initialize()).
CSession session;
hr = spAdmin->CreateDataSource(2, dbinit, NULL, IID_IOpenRowset, (IUnknown**) &session);
if (FAILED(hr))
return hr;
//
// Using SQL DDL commands for this as the Ole DB interfaces are a coding
// nightmare to use.
CCommand<CNoAccessor, CNoRowset> cmd;
TCHAR szSQL[] = _T("CREATE TABLE MyTable (Field1 INTEGER, Field2 TEXT(20))");
hr = cmd.Open(session, szSQL); // this executes the SQL command in szSQL
if (FAILED(hr))
return hr;
ps: this is probably much easier to do in ADO, but you'll have to ask someone else about that :-)
Why not use ADO?
Thanks for the response. I notice your code works with Access97. Do you know if it'll also work with 2000? One of the reasons I wanted to use OLE DB was to try to make my app version independent.
What can you do when you have to rely on Microsoft?
Thanks again..........Paul
When creating a database the driver will always create it in its "native" format unless you specify otherwise through the JETOLEDB_ENGINETYPE property. This is what the code i posted is doing to force the driver to create it in Access 97 format, if i left out the JETOLEDB_ENGINETYPE property i would have got Access 2000 format. When opening a database the driver will open it in whatever format it was created and saved in.
So your app will have to choose one particular Access version to create new databases in, but it will have no trouble opening older versions as long as you dont do anything too fancy ;-)
how do u did that opening a *.mdb using DAO:)
You have to use ADOX not ADO. ADOX has all the functionality to create .mdb file on the fly.