“Value cannot be null. (Parameter ‘path1’)” on connectionString

huangapple go评论83阅读模式
英文:

"Value cannot be null. (Parameter 'path1')" on connectionString

问题

When debugging,我可以成功连接到SQLite数据库。但是在构建.NET应用程序后,SQLite在使用 system.IO.Path.Combine 时出现问题:

在 System.IO.Path.Combine(String path1, String path2) 处
在 System.Data.SQLite.SQLiteConnection..ctor(String connectionString, Boolean parseViaFramework) 处
在 LDF_DetectionTool.DatabaseConnector.GetApplicationsList() 中的 SomePath\DatabaseConnector.cs:第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并再次构建,以及在第23行将 parseViaFramework 设置为true和false(new SQLiteConnection(connectionString, false))。

英文:

When debugging I can make a successful connection to an SQLite database. But after building the .NET application SQLite has trouble using 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

The code :

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&lt;string&gt; GetApplicationsList()
        {
            string databaseFileName = &quot;Databases\\LDF_DETECTION_TOOL_DATA.db&quot;;
            string databaseFilePath = AppDomain.CurrentDomain.BaseDirectory + databaseFileName;
            string connectionString = &quot;Data Source=&quot; + databaseFilePath;

            List&lt;string&gt; applicationList = new List&lt;string&gt;();
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection(connectionString, true))
                {
                    connection.Open();

                    string query = &quot;SELECT * FROM APPLICATIONS&quot;;
                    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;
        }

The exception message :

> Value cannot be null. (Parameter 'path1')

None of the variables are null (I can display them in messageboxes, even after building). There is something wrong after build which works when debugging. The database is in the right location.

I tried reinstalling the nuget package, removing my own use of Path.Combine (already gone in above code), rebuilding several times, reboot Visual Studio and build again and setting parseViaFramework to true and false (new SQLiteConnection(connectionString, false) on line 23).

答案1

得分: 2

I ran into the same problem.

It appears to occur when I specify the PublishSingleFile property to csproj in an application using System.Data.SQLite.Core.

As a result, I solved it by setting the IncludeNativeLibrariesForSelfExtract property to true.

As René pointed out, it seems to be an issue with Assembly.GetExecutingAssembly().Location. In the single executable publish format, the current path of the assembly cannot be retrieved because the managed DLLs are extracted and loaded in memory.

The following page says that by setting the IncludeNativeLibrariesForSelfExtract property to true, all files including managed assemblies will be extracted to a folder (probably a temporary folder).

Link to the documentation page

Only managed DLLs are bundled with the app into a single executable.
When the app starts, the managed DLLs are extracted and loaded in memory,
avoiding the extraction to a folder.
With this approach, the managed binaries are embedded in the single file bundle,
but the native binaries of the core runtime itself are separate files.

To embed those files for extraction and get one output file,
set the property IncludeNativeLibrariesForSelfExtract to true.

Specifying IncludeAllContentForSelfExtract extracts all files,
including the managed assemblies, before running the executable.
This may be helpful for rare application compatibility problems.

英文:

I ran into the same problem.

It appears to occur when I specify the PublishSingleFile property to csproj in an application using System.Data.SQLite.Core.

As a result, I solved it by setting the IncludeNativeLibrariesForSelfExtract property to true.

As René pointed out, it seems to be an issue with Assembly.GetExecutingAssembly().Location. In the single executable publish format, the current path of the assembly cannot be retrieved, because the managed DLLs are extracted and loaded in memory.

The following page says that by setting the IncludeNativeLibrariesForSelfExtract property to true, all files including managed assemblies will be extracted to a folder (probably a temporary folder).

https://learn.microsoft.com/en-us/dotnet/core/deploying/single-file/overview?tabs=cli

> Only managed DLLs are bundled with the app into a single executable.
> When the app starts, the managed DLLs are extracted and loaded in memory,
avoiding the extraction to a folder.
> With this approach, the managed binaries are embedded in the single file bundle,
> but the native binaries of the core runtime itself are separate files.
>
> To embed those files for extraction and get one output file,
> set the property IncludeNativeLibrariesForSelfExtract to true.
>
> Specifying IncludeAllContentForSelfExtract extracts all files,
> including the managed assemblies, before running the executable.
> This may be helpful for rare application compatibility problems.

答案2

得分: 0

I had the same problem few weeks early.
我几周前遇到了同样的问题。

It looks for Assembly.GetExecutingAssembly().Location.
它查找 Assembly.GetExecutingAssembly().Location。

My code was in a single file windows service so this location is an empty string.
我的代码位于一个单一文件的Windows服务中,因此该位置是一个空字符串。

In debug it was not empty.
在调试模式下它不是空的。

Hope this helps.
希望这能帮助。

英文:

I had the same problem few weeks early.

It looks for Assembly.GetExecutingAssembly().Location.

My code was in a single file windows service so this location is an empty string.

In debug it was not empty.

Hope this helps.

huangapple
  • 本文由 发表于 2023年6月30日 00:39:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583021.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定