C# 在不实际打开数据库的情况下访问MSAccess属性

C# 在不实际打开数据库的情况下访问MSAccess属性,c#,ms-access,reflection,late-binding,C#,Ms Access,Reflection,Late Binding,我尝试的是在不实际打开数据库的情况下访问MS access属性 下面是一些代码,可以更好地理解: var processStartInfo = new ProcessStartInfo(args[0]) { WindowStyle = ProcessWindowStyle.Hidden, CreateNoWindow = true }; Process.Start(processStartInfo); application = (Acc

我尝试的是在不实际打开数据库的情况下访问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启动文件

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

是否有可能通过反射和后期绑定这样的方式遍历属性:


或者是否有任何其他选项可以实现我想要的功能?

很抱歉,我没有详细信息,但正在研究使用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

以防有人使用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();
            }
        }
    }
}


使用API可以模拟Shift键,它可以停止启动,但我不知道如何将其应用于c#::@Remou。是的,这是可能的,但没有好处,因为我想要的是一个程序,可以激活和停用旁路键(shift)。一旦我停用了它,模拟就没用了。DAO将在ACCDB上工作,而不仅仅是MDB。它是所有Access数据库的本机接口库。但是,如果是ACCDB,则必须使用ACE,而不是Jet 4。事实上,如果您已经在机器上安装了ACE,那么最好在默认情况下使用它,因为它可以同时处理这两个问题。