1

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

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

4

2 回答 2

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
于 2011-02-10T18:56:05.993 回答
1

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

于 2011-02-15T17:57:23.030 回答