在调试时,我可以成功连接到SQLite数据库。但是在构建.NET应用程序后,SQLite在使用system.IO.Path.Combine
时遇到了问题:
at System.IO.Path.Combine(String path1, String path2)
at System.Data.SQLite.SQLiteConnection..ctor(String connectionString, Boolean parseViaFramework)
at LDF_DetectionTool.DatabaseConnector.GetApplicationsList() in SomePath\DatabaseConnector.cs:line 23
字符串
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
using System.Data;
namespace LDF_DetectionTool
{
internal class DatabaseConnector
{
public List<string> GetApplicationsList()
{
string databaseFileName = "Databases\\LDF_DETECTION_TOOL_DATA.db";
string databaseFilePath = AppDomain.CurrentDomain.BaseDirectory + databaseFileName;
string connectionString = "Data Source=" + databaseFilePath;
List<string> applicationList = new List<string>();
try
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString, true))
{
connection.Open();
string query = "SELECT * FROM APPLICATIONS";
using (SQLiteCommand command = new SQLiteCommand(query, connection))
{
using (SQLiteDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
applicationList.Add(reader.GetString(0));
}
}
}
connection.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(connectionString);
MessageBox.Show(ex.Message);
MessageBox.Show(ex.StackTrace);
}
return applicationList;
}
型
异常消息:
值不能为空。(参数'path1')
没有一个变量是空的(我可以在消息框中显示它们,即使在构建之后)。在调试时工作的构建后有一些错误。数据库位于正确的位置。
我尝试重新安装nuget包,删除我自己使用的Path.Combine
(已经在上面的代码中),重建几次,重新启动Visual Studio并再次构建,并将parseViaFramework
设置为true和false(new SQLiteConnection(connectionString, false)
在第23行)。
2条答案
按热度按时间9ceoxa921#
我在几周前遇到了同样的问题。
它查找Assembly.GetExecutingAssembly().Location。
我的代码是在一个单一的文件窗口服务,所以这个位置是一个空字符串。
在debug中,它不是空的。
希望这对你有帮助。
wfauudbj2#
我也遇到了同样的问题。
在使用
System.Data.SQLite.Core
的应用程序中,当我将PublishSingleFile
属性指定给csproj时,似乎会发生这种情况。因此,我通过将
IncludeNativeLibrariesForSelfExtract
属性设置为true
来解决这个问题。正如René所指出的,这似乎是
Assembly.GetExecutingAssembly().Location
的一个问题。在单一可可执行出版格式中,无法撷取组件的目前路径,因为Managed DLL会撷取并载入内存中。下一页指出,将
IncludeNativeLibrariesForSelfExtract
属性设定为true
,所有档案(包括Managed组件)都会解压缩至文件夹(可能是缓存文件夹)。https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli
只有Managed DLL会与应用程序捆绑在单一可可执行中。当应用程序启动时,托管DLL将被提取并加载到内存中,从而避免提取到文件夹。通过这种方法,托管二进制文件嵌入到单个文件包中,但核心运行时本身的本机二进制文件是单独的文件。
若要嵌入这些档案以进行解压缩并取得一个输出档,请将IncludeNativeLibrariesForSelfExtract属性设定为true。
指定IncludeAllContentForSelfExtract会在执行可可执行之前,撷取所有档案,包括Managed组件。这可能有助于解决极少出现的应用程序兼容性问题。