通过 WPF 应用程序从 Access (.accdb) 检索查询、窗体和报表属性



有没有办法从.accdb文件中获取对象(查询、窗体、报表、宏等(?我不是在寻找存储数据,我想检查这些对象的结构和设计。

编辑:使问题清楚。我想从 C# 访问这些对象,以便我可以进行自动检查。

以下 C# 控制台应用列出了指定窗体中的所有控件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace comAutoTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // this code requires the following COM reference in the project:
            // Microsoft Access 14.0 Object Library
            //
            var objAccess = new Microsoft.Office.Interop.Access.Application();
            objAccess.Visible = false;
            objAccess.OpenCurrentDatabase(@"C:UsersPublicDatabase1.accdb");
            string formName = "MembersForm";
            Console.WriteLine(String.Format("The form [{0}] contains the following controls:", formName));
            objAccess.DoCmd.OpenForm(formName, Microsoft.Office.Interop.Access.AcFormView.acDesign);
            Microsoft.Office.Interop.Access.Form frm = objAccess.Forms[formName];
            foreach (Microsoft.Office.Interop.Access.Control ctl in frm.Controls)
            {
                Console.WriteLine();
                Console.WriteLine(String.Format("    [{0}]", ctl.Name));
                Console.WriteLine(String.Format("        {0}", ctl.GetType()));
            }
            objAccess.DoCmd.Close(Microsoft.Office.Interop.Access.AcObjectType.acForm, formName);
            objAccess.CloseCurrentDatabase();
            objAccess.Quit();
            Console.WriteLine();
            Console.WriteLine("Done.");
        }
    }
}

输出为:

The form [MembersForm] contains the following controls:
    [LastName]
        Microsoft.Office.Interop.Access.TextBoxClass
    [Label0]
        Microsoft.Office.Interop.Access.LabelClass
    [MemberDonationsSubform]
        Microsoft.Office.Interop.Access.SubFormClass
    [MemberDonationsSubform Label]
        Microsoft.Office.Interop.Access.LabelClass
    [Command3]
        Microsoft.Office.Interop.Access.CommandButtonClass
Done.

编辑:对于关系,做这样的事情

Microsoft.Office.Interop.Access.Dao.Database cdb = objAccess.CurrentDb();
foreach (Microsoft.Office.Interop.Access.Dao.Relation rel in cdb.Relations)
{
    Console.WriteLine(rel.Name);
}

Access 中有一项功能称为"数据库文档"。可以通过"数据库工具"功能区面板访问它。当您运行该工具时,它将生成一个名为"对象定义"的报告。您可以打印它,但我建议您将其导出到某种文件(例如Excel或文本(。这样就更容易分析了。

最新更新