如何通过C#程序修改MS Access数据库属性集合(而不是数据!)? [英] How to modify MS Access database Properties collection (not data!) from a C# program?

查看:93
本文介绍了如何通过C#程序修改MS Access数据库属性集合(而不是数据!)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Visual Studio 2010中的C#访问Microsoft Access Database对象的属性(如在CurrentDb.Properties中)的最佳方法是什么?

What is the best way to access Microsoft Access Database object's Properties (like in CurrentDb.Properties) from C# in Visual Studio 2010?

(不是必不可少的:实际上,我想在按需"几十个数据库中摆脱 Replication .复制已经有几年没有使用了,对于MS Access来说还可以在2013年之前.Access2013拒绝具有此功能的数据库.)

(Not essential: In fact I want to get rid of Replication in a few dozen databases "on demand". Replication is not in use for a few years, and it was OK for MS Access prior to 2013. Access 2013 rejects databases with this feature.)

推荐答案

您可以使用以下代码遍历数据库中的属性以及不同容器及其属性,以及遍历数据库容器中的Documents及其属性.输出将写入调试输出"窗口.

The following code iterates over the properties in the database as well over the different containers and its properties as well over the Documents and its properties in the Databases container. The output is written to the Debug Output window.

此后,我选择了 Property 来自不同的属性集合并更改其值.请注意,如果属性不存在,则在集合上使用索引器会引发异常.

After that I pick a Property from different property collections and change it's value. Do note that using the indexer on the collection will throw an exception if the property doesn't exist.

确保您具有对 Microsoft Office 12.0 Access数据库引擎对象库的主互操作程序集的引用(您的版本信息有所不同),以便在using语句中具有以下内容:

Make sure you have a reference to the Primary Interop Assembly for Microsoft Office 12.0 Access database engine Object Library (your version migth vary) so that you can have the following in your using statements:

using Microsoft.Office.Interop.Access.Dao;

您的方法将如下所示:

    // Open a database
    var dbe = new DBEngine();
    var db = dbe.OpenDatabase(@"C:\full\path\to\your\db\scratch.accdb");
    // Show database properties
    DumpProperties(db.Properties);
    // show all containers
    foreach (Container c in db.Containers)
    {
        Debug.WriteLine("{0}:{1}", c.Name, c.Owner);
        DumpProperties(c.Properties);
    }
    //Show documents and properties for a specific container
    foreach (Document d in db.Containers["Databases"].Documents)
    {
        Debug.WriteLine("--------- " + d.Name);
        DumpProperties(d.Properties);
    }

    // set a property on the Database
    Property prop = db.
        Properties["NavPane Width"];

    prop.Value = 300;

    // set a property on the UserDefined document
    Property userdefProp = db
        .Containers["Databases"]
        .Documents["UserDefined"]
        .Properties["ReplicateProject"];

    userdefProp.Value = true;

财产倾销者帮手

private static void DumpProperties(Properties props)
{
    foreach (Property p in props)
    {
        object val = null;
        try
        {
            val = (object)p.Value;
        }
        catch (Exception e)
        {
            val = e.Message;
        }
        Debug.WriteLine(
            "{0} ({2}) = {1}", 
            p.Name, 
            val, 
            (DataTypeEnum) p.Type);
    }
}

我使用来克服动态类型引发的异常(因为Value属性原来是是)

I used this to overcome an exception being thrown on dynamic types (as the Value property turns out to be)

这篇关于如何通过C#程序修改MS Access数据库属性集合(而不是数据!)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆