【发布时间】:2013-11-22 03:42:42
【问题描述】:
有没有办法从.accdb 文件中获取对象(查询、表单、报告、宏等)?我不是在寻找存储数据,我想检查这些对象的结构和设计。
编辑:让问题更清楚。我想从 C# 中访问这些对象,以便进行自动检查。
【问题讨论】:
有没有办法从.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:\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,但我想知道哪里有办法在不重新启动的情况下解锁数据库。以防万一我在测试其他功能时发生这种情况。
MSACCESS.EXE?
Access 中有一个称为“数据库文档”的功能。您可以通过“数据库工具”功能区面板访问它。当您对所有对象运行该工具时,它将生成一个名为“对象定义”的报告。您可以打印它,但我建议您将其导出到某种文件(例如 Excel 或文本)。这样分析起来会更容易。
【讨论】: