简体   繁体   English

System.InvalidOperationException:必须设置值。 为SQLite设置Null参数

[英]System.InvalidOperationException: Value must be set. Setting Null Parameters for SQLite

I am using Microsoft.Data.Sqlite 2.1.0 on .NETStandard 2.0 and .NET Core 2.1.0 to interact with a local SQLite database. 我在.NETStandard 2.0和.NET Core 2.1.0上使用Microsoft.Data.Sqlite 2.1.0与本地SQLite数据库进行交互。 SQLitePCL is mentioned in the exception and is also a dependency. SQLitePCL在异常中提到,也是一个依赖项。

I want to be able to set a parameter's value to NULL but when I do that, I get an exception that the parameter's "value must be set". 我希望能够将参数的值设置为NULL但是当我这样做时,我得到一个异常,即参数的“值必须设置”。

SqliteCommand cd = cn.CreateCommand();
cd.CommandText = sql;
cd.Parameters.AddWithValue("@param1", null); //This fails
cd.Parameters.AddWithValue("@param2", DBNull.Value); //This also fails
cd.Parameters.AddWithValue("@param3", ""); //This insert an empty string, not a NULL
cd.ExecuteNonQuery(); //The exception is thrown on this line

Full exception: 完全例外:

{System.InvalidOperationException: Value must be set.
  at Microsoft.Data.Sqlite.SqliteParameter.Bind (SQLitePCL.sqlite3_stmt stmt) [0x0004c] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at (wrapper remoting-invoke-with-check) Microsoft.Data.Sqlite.SqliteParameter.Bind(SQLitePCL.sqlite3_stmt)
  at Microsoft.Data.Sqlite.SqliteParameterCollection.Bind (SQLitePCL.sqlite3_stmt stmt) [0x00017] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at (wrapper remoting-invoke-with-check) Microsoft.Data.Sqlite.SqliteParameterCollection.Bind(SQLitePCL.sqlite3_stmt)
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader (System.Data.CommandBehavior behavior) [0x0025d] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at Microsoft.Data.Sqlite.SqliteCommand.ExecuteReader () [0x00000] in <56cfa09aae23467e945f1a64a1f893bb>:0 
  at MyApp.DbHelper.BuildDataSet (Microsoft.Data.Sqlite.SqliteCommand cd) [0x00019] in C:\...\MyApp\DbHelper.cs:55 }

According to the official documentation , the value "can be null." 根据官方文档 ,值“可以为null”。

I've tried creating a new SqliteParameter and setting the value to null there, thinking perhaps there's an issue with AddWithValue(), but that yields the same result. 我已经尝试创建一个新的SqliteParameter并将值设置为null,这可能是因为AddWithValue()可能存在问题,但这会产生相同的结果。

How do I set a SqliteParameter's value to NULL ? 如何将SqliteParameter的值设置为NULL

I've had more experience with SQLite now and have settled on an answer through experience. 我现在有更多的SQLite经验,并通过经验确定了答案。

I'm unsure now of the exact situation that was causing me problems when I originally posted this question. 我现在不确定当我最初发布这个问题时导致我出现问题的确切情况。

Contrary to the opening question, this does indeed work: 与开场问题相反,这确实有效:

cd.Parameters.AddWithValue("@param2", DBNull.Value);

A straight-up null will throw the value must be set exception. 一个直接的null将抛出value must be set异常。 If a parameter value is null , I am now detecting that and converting it to DBNull.Value in a wrapper class. 如果参数值为null ,我现在检测到并将其转换为包装类中的DBNull.Value

This solution has been very reliable for me for the the past month. 在过去的一个月里,这个解决方案对我来说非常可靠。

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

相关问题 System.InvalidOperationException必须包含ViewModel的值 - System.InvalidOperationException must contain value for ViewModel System.InvalidOperationException,同时检查空值 - System.InvalidOperationException while checking null value System.InvalidOperationException:MySQL 连接中的“连接属性不能为空” - System.InvalidOperationException:'Connection Property must not be null' in MySQL Connection System.InvalidOperationException:“必须在调用“PrepareDocumentRenderer”之前设置“DocumentRenderer”。 - System.InvalidOperationException: ''DocumentRenderer' must be set before calling 'PrepareDocumentRenderer'.' System.InvalidOperationException设置texbox文本 - System.InvalidOperationException setting texbox text System.InvalidOperationException:无法设置可见性 - System.InvalidOperationException: Can not set Visibility System.InvalidOperationException:Nullable对象必须具有value.ASP.NET MVC - System.InvalidOperationException: Nullable object must have a value.ASP.NET MVC System.InvalidOperationException 保存 - System.InvalidOperationException on saving System.InvalidOperationException: &#39;连接必须有效且打开。&#39; - System.InvalidOperationException: 'Connection must be valid and open.' 实体框架中的system.InvalidOperationException - system.InvalidOperationException in entityframework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM