2011-02-10 60 views
1

我想要的是访问MS Access属性,而不实际打开数据库。访问MSAccess属性,而不实际打开数据库

下面是一些代码,以获得更好的理解:

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()属性)和MS Access启动的东西踢。

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

是否有可能通过属性,可能是反射和后期绑定:http://www.codeproject.com/KB/database/mdbcompact_latebind.aspx

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

+0

API可以模拟Shift键来停止启动的东西,但我不知道它如何适应c#:http://www.mvps.org/access/ api/api0068.htm – Fionnuala 2011-02-10 21:54:13

+0

@ Remou。是的,这是可能的,但没有任何好处,因为我想要的是一个可以激活和停用bypasskey(shift)的程序。一旦我停用它,模拟将无济于事。 – Egi 2011-02-14 11:52:44

回答

1

对不起,我没有细节,但调查使用DAO打开Access数据库(假设它是2007年以前)。 DAO是本机访问/喷射代码,因此您不必实际启动整个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 
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(); 
      } 
     } 
    } 
} 

相关问题