【问题标题】:Retrieve query, form, and report properties from Access (.accdb) via a WPF application通过 WPF 应用程序从 Access (.accdb) 中检索查询、表单和报表属性
【发布时间】:2013-11-22 03:42:42
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net wpf ms-access


    【解决方案1】:

    以下 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);
    }
    

    【讨论】:

    • 正是我需要的。非常感谢!
    • 还有一个问题。当我使用Application.ExportXML 时,我犯了一些愚蠢的错误。然后数据库被锁定。每次发生这种情况时,我都必须重新启动。虽然我现在可以毫无问题地使用Application.ExportXML,但我想知道哪里有办法在不重新启动的情况下解锁数据库。以防万一我在测试其他功能时发生这种情况。
    • @Martin 您是否尝试进入 Windows 任务管理器并从“进程”选项卡中杀死 MSACCESS.EXE
    • 我做了,但我没有看到任何 MSACCESS.EXE 进程。我看到了 MyProgram.vhost.exe(我认为就是这样),但我无法杀死它。我也试过解锁器。在unlocker解锁accdb文件后,我可以删除laccdb。但是我仍然无法在不重新启动的情况下打开数据库。
    • 我正在使用我的家用计算机并杀死 MSACCESS.EXE 可以解决问题。可能是因为我不是那台计算机的管理员。我可以获得表格、表格等。我如何获得关系?我检查了访问对象模型参考,但没有发现任何有用的东西。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-20
      • 1970-01-01
      • 1970-01-01
      • 2014-05-07
      • 2019-10-01
      • 1970-01-01
      相关资源
      最近更新 更多