简体   繁体   English

我将 c# windows 应用程序与 Access 数据库 (accdb) 连接,但该应用程序在其他计算机上不起作用

[英]I connected c# windows app with Access database (accdb) but the application doesn't work on other computers

I connected an Access database (accdb) with C#-windows application project.我将 Access 数据库 (accdb) 与 C#-windows 应用程序项目连接起来。 The "accdb" database is located in a folder in desktop. “accdb”数据库位于桌面的文件夹中。 It works correctly on my computer but when i build a setup file and installed it on other computer the software didn't work.它在我的计算机上正常工作,但是当我构建一个安装文件并将其安装在其他计算机上时,该软件无法正常工作。 (i know the problem is that the database located in the folder) but i dont't know how to change the code that after installation on other computer, it can still connect to the database. (我知道问题是数据库位于文件夹中)但我不知道如何更改代码,在其他计算机上安装后,它仍然可以连接到数据库。 Does anyone know how should i solve this problem?有谁知道我应该如何解决这个问题? Here is the simple connection that i wrote:这是我写的简单连接:

OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\aa\Test.accdb;Jet OLEDB:Database Password=123;");

It works correctly on my computer but when i build a setup file and installed it on other computer the software didn't work.它在我的计算机上正常工作,但是当我构建一个安装文件并将其安装在其他计算机上时,该软件无法正常工作。

The primary reason for this is because that path doesn't exist on the other machine;造成这种情况的主要原因是该路径在另一台机器上不存在; you've hard-coded your path.你已经对你的路径进行了硬编码。

 OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\aa\Test.accdb;Jet OLEDB:Database Password=123;");

C:\\Users\\aa\\Test.accdb this is the actual issue, you shouldn't hard code this value, instead you have two options I can think of. C:\\Users\\aa\\Test.accdb这是实际问题,你不应该硬编码这个值,相反你有两个我能想到的选项。

  1. Look for the file along side the application where it is being executed from (this requires the file to be inside the same directory the exe is in).在执行它的应用程序旁边查找文件(这要求文件位于 exe 所在的同一目录中)。
  2. You could allow the end user to enter the location of that file, if it exist, save this path to use again when needed.您可以允许最终用户输入该文件的位置,如果存在,请保存此路径以在需要时再次使用。

You can use either or I mention above and or do both of them, your choice.你可以使用上面提到的或我提到的,或者两者都使用,你的选择。 Below is a simple example using option one above.以下是使用上述选项一的简单示例。

using System.Reflection;
using System.IO; 

public static string GetDBConnection()
{
   try
   {
      string dbExecPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Test.accdb");
      return $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ dbExecPath };Jet OLEDB:Database Password=123;";
   }
   catch (Exception)
   {
      return string.Empty;
   }            
}

This get's the assemblies location (path) and combines it with your file name (db file).这将获取程序集位置(路径)并将其与您的文件名(db 文件)组合在一起。 Then combine's that with your other connection string parts and return's the whole connection string.然后将它与您的其他连接字符串部分结合起来,并返回整个连接字符串。

Please Note: the namespaces that have to be used and the db file must be in the same directory that the exe is if going this route.请注意:如果要走这条路,必须使用的命名空间和 db 文件必须与 exe 位于同一目录中。

Now you can call it like this:现在你可以这样称呼它:

 OleDbConnection con = new OleDbConnection(GetDBConnection());

You may want to assign the GetDBConnection() to a var and check if it's empty before constructing your connection, it may be empty.您可能希望将GetDBConnection()分配给 var 并在构建连接之前检查它是否为空,它可能为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM