在Visual C ++中创建Access数据库

时间:2019-05-23 13:18:25

标签: ms-access visual-c++ odbc ado dao

当前,我需要找到一种在Visual C ++ 2008中创建Access数据库的方法,以便:

  1. 我可以以不同的格式创建Access数据库,包括Access 95/97格式,2000格式和2007 accdb格式。
  2. 我不仅可以在Access数据库中创建表,还可以创建其他对象,包括查询,报表,表单和宏。
  3. 向表中添加大量记录时,该方法具有更好的性能。

我已经在网上搜索过,并且发现了诸如this之类的资源,其中列出了一些方法,但是据我所知,DAO似乎已被弃用。

其他方法呢?

谢谢

1 个答案:

答案 0 :(得分:2)

据我了解,您想通过调用一些函数和过程来创建数据库及其中的对象。我心中最好的方法是使用DAO库。这是VBScript示例:

Sub CreateDatabaseFile(ByVal strDbPath) 
    Dim wspDefault 'As Workspace
    Dim dbs      ' As Database
    Dim dbEngine ' DAO DB Engine    
    Dim dbLangGeneral
    dbLangGeneral = ";LANGID=0x0409;CP=1252;COUNTRY=0"
    Set dbEngine = CreateObject("DAO.DBEngine.120")
    Set wspDefault = DBEngine.Workspaces(0)
    Set dbs = wspDefault.CreateDatabase(strDbPath, dbLangGeneral)
    Dim s
    s="CREATE TABLE tblCustomers (CustomerID INTEGER CONSTRAINT PK_tblCustomers PRIMARY KEY, " _
    & " [Last Name] TEXT(50) NOT NULL, [First Name] TEXT(50) NOT NULL, Phone TEXT(10), Email TEXT(50), " _
    & " Address TEXT(40)) "
    dbs.execute s
    dbs.Close

    Dim AccessApp
    Set AccessApp = CreateObject("Access.Application")
    AccessApp.OpenCurrentDatabase strDbPath
    Dim frm 'As Form
    Dim ctlLabel 'As Control
    Dim  ctlText 'As Control
    Dim intDataX 'As Integer, intDataY As Integer
    Dim intDataY
    Dim intLabelX 'As Integer, intLabelY As Integer
    Dim intLabelY

    ' Create new form with tblCustomers table as its record source.
    Set frm = AccessApp.CreateForm
    'frm.Name = "MyForm"
    frm.RecordSource = "tblCustomers"

    ' Set positioning values for new controls.
    intLabelX = 100
    intLabelY = 100
    intDataX = 1000
    intDataY = 100

    ' Create unbound default-size text box in detail section.
    Dim acTextBox
    acTextBox = 109
    Set ctlText = AccessApp.CreateControl(frm.Name, acTextBox, , "", "", _
        intDataX, intDataY)
    ctlText.ControlSource = "Last Name"
    Dim acLabel
    acLabel = 100
    ' Create child label control for text box.
    Set ctlLabel = AccessApp.CreateControl(frm.Name, acLabel, , _
         ctlText.Name, "NewLabel", intLabelX, intLabelY)
    Dim acButton

    AccessApp.DoCmd.Save, frm.Name
    ' Restore form.
    AccessApp.DoCmd.Restore
    AccessApp.DoCmd.Quit
End Sub
Call CreateDatabaseFile("c:\test\a test database file.mdb")