通过WPF应用程序从Access(.accdb)检索查询,表单和报告属性

时间:2013-11-22 03:42:43

标签: c# .net wpf ms-access

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

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

2 个答案:

答案 0 :(得分:2)

以下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:\Users\Public\Database1.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);
}

答案 1 :(得分:0)

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