【问题标题】:Create Access Database in Visual C++在 Visual C++ 中创建 Access 数据库
【发布时间】:2019-05-23 13:18:25
【问题描述】:

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

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

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

其他方法呢?

谢谢

【问题讨论】:

  • DAO NOT 已被弃用,只有它的特定部分(连接到非 Access 数据库的 ODBCDirect 工作区)被弃用。它广泛传播错误信息。如果您想创建和操作的不仅仅是 Access 数据库中的表,则需要使用 DAO。
  • 你的意思是使用Direct DAO访问Access吗?在这篇文章docs.microsoft.com/en-us/previous-versions/office/developer/… 中,它说 MFC DAO 仅支持 Jet 引擎到 3.6。
  • @SimonMourier,似乎可以创建表单、查询等。见social.msdn.microsoft.com/Forums/office/en-US/…
  • 是的,但您需要在运行代码的位置安装 Access。是这样吗?
  • @alancc 确实,我的意思是直接 DAO/acedao.dll(它基于 COM,文档主要面向 VBA,但您应该能够轻松地将概念转换为 C++)。请注意,它需要在计算机上安装 Access 或 Access 数据库引擎。

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


【解决方案1】:

据我了解,您想通过调用一些函数和过程来创建数据库和其中的对象。我认为最好的方法是使用 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")

【讨论】:

  • DAO 并没有完全弃用,它会在更新 MS Access 期间更新其版本号
  • 抱歉,通过 DAO 创建表单、报表、模块、宏似乎是不可能的。唯一的方法是使用 Access 自动化来做到这一点?
  • 可以通过 DAO 创建任何您想要的表格、表单、元素。查看Access.Application对象的方法。
  • 谢谢。这就是访问自动化。
猜你喜欢
  • 1970-01-01
  • 2010-11-26
  • 2014-10-11
  • 2019-03-22
  • 2014-12-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
相关资源
最近更新 更多