我正在尝试访问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?
或者还有其他选择可以达到我想要的目的吗?
答案 0 :(得分: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 :(得分: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();
}
}
}
}