C# 部署c期间出现连接字符串错误#

C# 部署c期间出现连接字符串错误#,c#,C#,程序部署上的连接字符串错误 这个问题有解决办法吗 尝试过了,但我认为这仅适用于sql吗 OleDbConnection MyConN= new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\3d_ServeR\Documents\Visual Studio 2013\Projects\POS\POS\POSDb.accdb"); 必须指定数据库文件的正确路径,如下所示: OleDbConnect

程序部署上的连接字符串错误 这个问题有解决办法吗

尝试过了,但我认为这仅适用于sql吗

OleDbConnection MyConN= new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\3d_ServeR\Documents\Visual Studio 2013\Projects\POS\POS\POSDb.accdb");

必须指定数据库文件的正确路径,如下所示:

OleDbConnection MyConN_Deploy = new OleDbConnection
    {
        ConnectionString = "Data Source=" + System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\POSDb.accdb"
    };

任何时候出现“连接字符串错误”时,您都需要这样做。将数据库与EXE放在同一文件夹中是很奇怪的,因为程序文件中的文件通常不可由非管理员用户编辑。可能是由于路径中的空格;另见此处:
//If it's on the same folder as the executable then 
//you can set it to the file name without specifying a path.
var dbPath = "Path\\To\\Your\\DB\\POSDb.accdb";

OleDbConnection MyConN= new OleDbConnection($"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={dbPath}");