简体   繁体   English

DBNull中的内容

[英]What in the DBNull

I have a stored procedure which has a parameter that is nullable to a decimal column. 我有一个存储过程,该存储过程的参数可以为小数列为空。 The method I wrote expects a Dictionary<string, string> to pass the parameter name and the value. 我编写的方法期望Dictionary<string, string>传递参数名称和值。

However, when I use reflection to GetValue when I do: 但是,当我在执行GetValue时使用反射时:

collection.Add(model.Name, DBNull.Value.ToString());

In essence all DBNull.Value returns would be a String.Empty . 本质上,所有DBNull.Value返回都将是String.Empty So how can I ensure the null is correctly passed to SQL Server? 那么,如何确保将null正确传递给SQL Server? Otherwise that will throw an exception, because invalid column data type. 否则会抛出异常,因为列数据类型无效。 The documentation states, with ToString it will return String.Empty . 文档指出,使用ToString它将返回String.Empty

DBNull is a singleton class, which means only this instance of this class can exist. DBNull是一个单例类,这意味着只能存在此类的该实例。 If a database field has missing data, you can use the DBNull.Value property to explicitly assign a DBNull object value to the field. 如果数据库字段缺少数据,则可以使用DBNull.Value属性为该字段显式分配DBNull对象值。 However, most data providers do this automatically. 但是,大多数数据提供程序会自动执行此操作。 To evaluate database fields to determine whether their values are DBNull, you can pass the field value to the DBNull.Value.Equals method. 要评估数据库字段以确定它们的值是否为DBNull,可以将字段值传递给DBNull.Value.Equals方法。 However, this method is rarely used because there are a number of other ways to evaluate a database field for missing data. 但是,这种方法很少使用,因为还有许多其他方法可以评估数据库字段中是否缺少数据。 These include the Visual Basic IsDBNull function, the Convert.IsDBNull method, the DataTableReader.IsDBNull method, the IDataRecord.IsDBNull method, and several other methods. 这些包括Visual Basic IsDBNull函数,Convert.IsDBNull方法,DataTableReader.IsDBNull方法,IDataRecord.IsDBNull方法以及其他几种方法。

This would be added to the database in the following manner: 可以通过以下方式将其添加到数据库中:

command.Parameters.AddWithValue(parameter.Key, parameter.Value);

A traditional Ado.Net SqlDataReader . 传统的Ado.Net SqlDataReader

I think you will have to get each value from the Dictionary and then use code like that shown below before passing the parameter to the command. 我认为您必须从Dictionary中获取每个值,然后在将参数传递给命令之前使用如下所示的代码。 In other words, store null as the value in the Dictionary, not DBNull, then call the code below to convert from null to DBNull.Value. 换句话说,将空值存储为Dictionary中的值,而不是DBNull,然后调用下面的代码将空值转换为DBNull.Value。

        SqlParameter firstNameParam = new SqlParameter("FirstName", System.Data.SqlDbType.NVarChar);
        string firstNameValue = null; // this is the value from the Dictionary
        if (firstNameValue == null) {
            firstNameParam.Value = DBNull.Value;
        }
        else {
            firstNameParam.Value = firstNameValue;
        }

After refactoring, you could do something like this: 重构之后,您可以执行以下操作:

  public static object ToDBNull(object value) {
        if (value == null) {
            return DBNull.Value;
        }
        else {
            return value;
        }
    }

Then call ToDBNull function like this: 然后像这样调用ToDBNull函数:

        cmd.Parameters.AddWithValue("FirstName", ToDBNull(firstNameValue));

If you have Dictionary<string, string> , and you have a value in the dictionary such that: 如果您有Dictionary<string, string> ,并且在字典中有一个值,例如:

var dictionary = new Dictionary<string, string>() { { "hello", null } };

string data = dictionary["hello"];
var dec = data == null ? (decimal?)null : Convert.ToDecimal(data);

You could write a little extension method to make things easier for passing it to a command 您可以编写一些扩展方法,使将其传递给命令更容易

public static object ValueOrDbNull(this decimal? value)
{
    if (value.HasValue)
    {
        return value;
    }

    return DBNull.Value;
}

And then add it to your command as such: 然后像这样将其添加到您的命令中:

command.Parameters.Add("@your_proc_param", dec.ValueOrDbNull());

There shouldn't be any issues passing it to a procedure in this manner, since you said it's declared as a nullable decimal field. 以这种方式将其传递给过程应该没有任何问题,因为您说它被声明为可空的十进制字段。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM