1

App_Data我的解决方案的文件夹中有两个文件。两个文件都是.accdb文件,一个是表格文件,核心。我有另一个文件,其中包含存储的查询和一些我不再需要的 VBA 元素,但我需要存储的查询。此解决方案位于网络驱动器上,保存查询的文件和保存表的文件是链接的。

当我创建安装项目并安装应用程序时,我只需要包含查询的文件。问题是该文件链接回表文件的原始位置。我需要它来请求包含表的文件的位置,因为它将安装在另一台机器上,其中.accdb包含表的文件可能在任何地方。有没有办法OpenFileDialog让他们指出它的位置?

我目前有一个 N-Tier 应用程序,其中包含一个DAL获取存储在My.Settings. 该字符串是"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\App_Data\FPC_Reporting.accdb"安装中包含的包含存储查询的文件。但是,该文件仍然认为包含表的文件仍指向我的网络位置,但如前所述,它可能在任何地方,所以我想让它询问用户;安装后,了解包含表的本地文件所在的位置。

安装应用程序后以及断开网络驱动器后收到的错误是“N:\PROJECTS\FPC Reporting Tool\FPCReportBuilder\FPCReportBuilder\App_Data\FPC_Reporting_DATA.accdb”不是有效路径。确保路径名拼写正确,并且您已连接到文件所在的服务器。” 错误中显示的文件名是包含表的文件,这些表应该链接到包含连接字符串中显示的存储查询的文件。

4

2 回答 2

2

以下 C# 代码已经过测试并确认可在 Visual Studio 2010 中运行:

private void button1_Click(object sender, EventArgs e)
{
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        string RemoteDatabasePath = openFileDialog1.FileName;

        // the following code requires that the project have a COM reference to:
        // "Microsoft Office 14.0 Access Database Engine Object Library"

        // create the DBEngine object
        var dbe = new Microsoft.Office.Interop.Access.Dao.DBEngine();

        // open the local database file containing the linked table
        Microsoft.Office.Interop.Access.Dao.Database db = dbe.OpenDatabase(@"C:\__tmp\testData.accdb");

        // create a TableDef object for the linked table we want to update
        Microsoft.Office.Interop.Access.Dao.TableDef tbd = db.TableDefs["Products"];

        // update the .Connect property with the full path to the remote database
        tbd.Connect = ";DATABASE=" + RemoteDatabasePath;

        // refresh the table link
        tbd.RefreshLink();

        // test the new connection
        Microsoft.Office.Interop.Access.Dao.Recordset rs = db.OpenRecordset("SELECT * FROM Products", Microsoft.Office.Interop.Access.Dao.RecordsetTypeEnum.dbOpenSnapshot);
        MessageBox.Show(rs.Fields["ProductName"].Value);
        rs.Close();
    }
    this.Close();
}

编辑回复:评论

检查给定计算机上安装的 Access 数据库引擎 ("ACE") 的版本:

搜索文件ACEOLEDB.DLL

  1. 如果找到它,C:\Program Files\Common Files\Microsoft Shared\OFFICE14则安装 ACE,并且其版本与操作系统的“位”匹配:32 位 Windows 上的 32 位 ACE,64 位 Windows 上的 64 位 ACE。

  2. 如果找到,C:\Program Files (x86)\Common Files\microsoft shared\OFFICE14则 32 位 ACE 安装在 64 位 Windows 上。

任何使用 ACE 的应用程序都需要安装正确的版本:32 位应用程序需要 32 位版本的 ACE(即使在 64 位 Windows 上),64 位应用程序需要 64 位版本的 ACE。.NET针对“任何平台”的应用程序将需要与主机操作系统的“位”相匹配的 ACE 版本。

于 2013-05-28T11:58:03.453 回答
0

您可以尝试一种方法将您的 Access DB 与解决方案分离。将其从App_Data文件夹中删除。

在系统上创建一个DSN Data Source (ODBC)forMicrosoft Access Driver并通过网络指向 Access DB 文件。更好的是,如果您可以映射系统上的网络驱动器以在系统运行/用户登录时自动映射。

在您的代码中使用connection-string上面创建的DSN.

看看这个


编辑: 如果你想要的只是一个,OpenFileDialog那么你可以试试这个:

private void Button1_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
    openFileDialog1.InitialDirectory = "C:\\"; 

    openFileDialog1.Title = "Select Database"; 
    openFileDialog1.CheckFileExists = true; 
    openFileDialog1.CheckPathExists = true; 
    openFileDialog1.DefaultExt = "accdb"; 
    openFileDialog1.Filter = "Access DB files (*.accdb;*.mdb)|*.accdb;*.mdb"; 
    openFileDialog1.FilterIndex = 2; 
    openFileDialog1.RestoreDirectory = true; 
    openFileDialog1.ReadOnlyChecked = true; 
    openFileDialog1.ShowReadOnly = true; 
    if (openFileDialog1.ShowDialog() == DialogResult.OK) 
    { 
        // This will give you the selected file
        string file = openFileDialog1.FileName;
        string connectionString = "Some connection stuff; DATABASE=" + file;
        // Connect to the Access DB
    } 
}

请将此代码用作起点,而不是复制粘贴解决方案。

于 2013-05-28T09:16:51.150 回答