C# : return dbnull. 值或函数的 system.type?

标签 c# types dbnull system.type

我想创建一个函数 (c#):

int PutInt (int? x)
{
    if (x.HasValue)
        return x.Value;
    else
        return DBNull.Value;
}

但是没有从 int 到 DBNull.Value 的转换。 有什么方法可以创建这样的函数吗?

最佳答案

假设您的 DBMS 是 SQLServer,您可以将函数转换为

object PutInt (int? x)
{
    if (x.HasValue)
        return (object)x.Value;
    else
        return (object)DBNull.Value;
}

因为 SQLParamater Value 属性假定类型为对象。希望我能回答你的问题。

关于C# : return dbnull. 值或函数的 system.type?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5927384/

相关文章:

c# - 如何跳过 `foreach` 循环的迭代?

c# - 使用 Entity Framework Core 存储计算属性

c# - 为什么 Entity Framework 代码首先更改我的类型名称?

Python类变量把一个函数变成了一个方法,为什么?

c# - Roslyn 的 GetTypeByMetadataName() 和通用类型

c# - 在插入数据库时​​将空字符串设置为 null

c# - 无法将 DBNULL 值插入 sql server

c# - 我应该使用 MySQL Connector/ODBC 还是 System.Data.Odbc?

types - 什么是 `init : () -> (Model, Cmd Msg)` 注释?

c# - 为什么 IS NULL 有效但 SQL Parameter with DBNull.Value for DateTime 不起作用?