Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么部署我的应用时SQL Server CE无法识别DB_C#_Sql_.net_Sql Server Ce - Fatal编程技术网

C# 为什么部署我的应用时SQL Server CE无法识别DB

C# 为什么部署我的应用时SQL Server CE无法识别DB,c#,sql,.net,sql-server-ce,C#,Sql,.net,Sql Server Ce,我一直在为如何让我的应用程序在部署时工作而苦苦挣扎——它一直说,无论我做什么,该文件都是无效的: 我现在得到的是: 数据源=“\MyDb.sdf” 数据库应该位于可执行文件所在的目录中。您可能需要使用绝对路径来访问文件 var pathToExe = System.Reflection.Assembly.GetExecutingAssembly().Location; var path = Path.GetDirectoryName(pathToExe); var pathToDb = Path

我一直在为如何让我的应用程序在部署时工作而苦苦挣扎——它一直说,无论我做什么,该文件都是无效的: 我现在得到的是:

数据源=“\MyDb.sdf”


数据库应该位于可执行文件所在的目录中。

您可能需要使用绝对路径来访问文件

var pathToExe = System.Reflection.Assembly.GetExecutingAssembly().Location;
var path = Path.GetDirectoryName(pathToExe);
var pathToDb = Path.Combine(path, "MyDb.sdf");

pathToDb
现在应该是数据库对象的绝对路径,假设它总是在执行程序集的目录中。

这是一个相对路径,应用程序目录中是否存在
MyDb.sdf
呢?是的,它存在,我希望这样。嗯,在例外情况下,它显示了正确的路径,但仍然表示路径无效。我不想再次询问,但您绝对确定路径中明确存在文件
MyDb.sdf
?哦,实际上,您的代码返回文件夹和exe文件,因此输出us XX.exe\\MyDb.sdfI更新了答案,我忘了。Location返回了程序集的完整路径。该更新将修复它以删除
XX.exe
。是的,就是它:)谢谢