【问题标题】:Access MSAccess Properties without actually opening the Database在不实际打开数据库的情况下访问 MSAccess 属性
【发布时间】:2011-02-10 18:34:05
【问题描述】:

我正在尝试的是在不实际打开数据库的情况下访问 MS Access 属性。

这里有一些代码可以更好地理解:

var processStartInfo = new ProcessStartInfo(args[0]) 
    { 
        WindowStyle = ProcessWindowStyle.Hidden, 
        CreateNoWindow = true
    };

Process.Start(processStartInfo);

application = (Access.Application)Marshal.GetActiveObject("Access.Application");

dao.Property allowByPassKeyProperty = null;

foreach (dao.Property property in application.CurrentDb().Properties)
{
    if (property.Name == "AllowByPassKey")
    {
        allowByPassKeyProperty = property;
        break;
    }
}

我的问题是,在这种情况下,我打开数据库以查找属性 (application.CurrentDb().Properties) 并启动 MS Access 启动内容。

我想避免所有启动的东西,只为属性注入正确的值。

是否可以通过属性,也许像这样使用反射和后期绑定:http://www.codeproject.com/KB/database/mdbcompact_latebind.aspx

或者还有其他选择可以实现我想要的吗?

【问题讨论】:

  • 可以使用 API 来模拟 Shift 键,这会停止启动的东西,但我不知道这将如何适应 c#:mvps.org/access/api/api0068.htm
  • @Remou。是的,这是可能的,但没有任何好处,因为我想要的是一个可以激活和停用旁路键(shift)的程序。一旦我将其停用,模拟将无济于事。

标签: c# ms-access reflection late-binding


【解决方案1】:

抱歉,我没有详细信息,但正在研究使用 DAO 打开 Access 数据库(假设它是 2007 年之前的)。 DAO 是本机 access/jet 代码,因此您不必实际启动整个 Access 应用程序。

我到处都是旧的 VB.Net(是的,.Net)代码:

m_oEngine = New DAO.DBEngine

m_oEngine.SystemDB = sWorkgroupFile

m_oWorkspace = m_oEngine.CreateWorkspace("My Workspace", sUserName, sPassword, DAO.WorkspaceTypeEnum.dbUseJet)

m_oDatabase = m_oWorkspace.OpenDatabase(sDatabaseFile, bExclusive, bReadOnly)  ' Note DAO docs say the second parameter is for Exclusive

【讨论】:

  • DAO 将在 ACCDB 上工作,而不仅仅是 MDB。它是所有 Access 数据库的本机接口库。但是,如果是 ACCDB,则必须使用 ACE,而不是 Jet 4。确实,如果您的机器上安装了 ACE,最好默认使用它,因为它可以同时处理两者。
【解决方案2】:

以防万一有人使用 MS Access 并需要相同的程序,这里是代码。

程序可以在 MS Access *.mdb、*.mde 文件中切换 AllowBypassKey 属性。仅使用 MS Access 2003 进行了测试。如果您已停用该属性,并且您的 AutoExec 宏正在运行,它会非常有用。

namespace ToggleAllowBypassKey
{
    public class Program
    {
        private static DAO.DBEngine dbEngine;

        private static DAO.Database database;

        public static void Main(string[] args)
        {
            try
            {
                if (args.Length == 0)
                {
                    Console.WriteLine("Please enter an MS Access application path as a parameter!");
                    return;
                }

                dbEngine = new DAO.DBEngine();
                database = dbEngine.OpenDatabase(args[0]);

                DAO.Property allowBypassKeyProperty = null;

                foreach (dao.Property property in database.Properties)
                {
                    if (property.Name == "AllowBypassKey")
                    {
                        allowBypassKeyProperty = property;
                        break;
                    }
                }

                if (allowBypassKeyProperty == null)
                {
                    allowBypassKeyProperty = database.CreateProperty("AllowBypassKey", DAO.DataTypeEnum.dbBoolean, false, true);
                    database.Properties.Append(allowBypassKeyProperty);
                    Console.WriteLine("AllowBypassKey Property has been added. It's Value is '" + allowBypassKeyProperty.Value + "'");
                }
                else
                {
                    allowBypassKeyProperty.Value = !allowBypassKeyProperty.Value;
                    Console.WriteLine("AllowBypassKey set to '" + allowBypassKeyProperty.Value + "'!");
                }
            }
            catch(Exception exception)
            {
                Console.WriteLine("Exception Message: " + exception.Message);
                Console.WriteLine("Inner Exception:" + exception.InnerException);
            }
            finally
            {
                database.Close();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(database);
                database = null;

                System.Runtime.InteropServices.Marshal.ReleaseComObject(dbEngine);
                dbEngine = null;

                Console.WriteLine();
                Console.WriteLine("Press enter to continue ...");
                Console.ReadLine();
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 2022-01-10
    • 2017-09-10
    • 1970-01-01
    相关资源
    最近更新 更多