这失败了 There is no implicit conversion between 'null' and 'int'
long? myVar = Int64.Parse( myOtherVar) == 0 ? null : Int64.Parse( myOtherVar);
Run Code Online (Sandbox Code Playgroud)
但是,这成功了:
if( Int64.Parse( myOtherVar) == 0)
myVar = null;
else
myVar = Int64.Parse( myOtherVar);
Run Code Online (Sandbox Code Playgroud)
有没有办法让三元运算符成功?
我在 .NETStandard 2.0 和 .NET Core 2.1.0 上使用 Microsoft.Data.Sqlite 2.1.0 与本地 SQLite 数据库交互。SQLitePCL 是在异常中提到的,也是一个依赖项。
我希望能够将参数的值设置为,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
Run Code Online (Sandbox Code Playgroud)
完全例外:
{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 …Run Code Online (Sandbox Code Playgroud)