CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 1999
    Posts
    24

    Creating an *.mdb on the fly with OLE DB/ADO

    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



  2. #2
    Guest

    Re: Creating an *.mdb on the fly with OLE DB/ADO

    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



  3. #3
    Join Date
    May 1999
    Posts
    24

    Re: Creating an *.mdb on the fly with OLE DB/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



  4. #4
    Guest

    Re: Creating an *.mdb on the fly with OLE DB/ADO

    yes you can I do it


  5. #5
    Join Date
    May 1999
    Posts
    128

    Re: Creating an *.mdb on the fly with OLE DB/ADO

    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 :-)


  6. #6
    Join Date
    Aug 1999
    Posts
    586

    Re: Creating an *.mdb on the fly with OLE DB/ADO

    Why not use ADO?


  7. #7
    Join Date
    May 1999
    Posts
    24

    Re: Creating an *.mdb on the fly with OLE DB/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



  8. #8
    Guest

    Re: Creating an *.mdb on the fly with OLE DB/ADO

    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 ;-)



  9. #9
    Join Date
    Sep 2003
    Posts
    5

    can u help

    how do u did that opening a *.mdb using DAO

  10. #10
    Join Date
    Jul 2003
    Location
    USA
    Posts
    7
    You have to use ADOX not ADO. ADOX has all the functionality to create .mdb file on the fly.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured