C# 处理 Datarow DBNull 的最佳方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12507948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:29:12  来源:igfitidea点击:

Best way to handle Datarow DBNull

c#database

提问by A Developer

Possible Duplicate:
Best way to check if a Data Table has a null value in it

可能的重复:
检查数据表中是否有空值的最佳方法

I want to know what should be the way to check DBNull for a DataTable - DataRow values.

我想知道检查 DBNull 以获取 DataTable - DataRow 值的方法应该是什么。

Ex

前任

I have a DataRow which fetches information from database from rows type like :

我有一个 DataRow,它从数据库中获取来自行类型的信息,例如:

varchar, money, Int and so on.

varchar、money、Int 等。

What should be my (simple and sweet) approach to handle such situation.

处理这种情况的我的(简单而甜蜜的)方法应该是什么。

采纳答案by Shree

Try:

尝试:

foreach(DataRow row in table.Rows)
{
    object value = row["ColumnName"];
    if (value == DBNull.Value)
    {

    }
    else
    {
    }
}

回答by codingbiz

Try this

尝试这个

For varchar

对于 varchar

string val = dr["name"].ToString();

For int

对于整数

int? val = dr["status"] == DBNull.Value ? (int?) null : Convert.ToInt32(dr["status"]);

Do the same for Money, Decimalas done for intreplacing with respective .Nettypes

执行与替换相应类型相同的Money, Decimal操作int.Net

回答by Mehmet Osmanoglu

You can use an extension method like this;

您可以使用这样的扩展方法;

public static T GetValue<T>(this OracleDataReader reader, string fieldName)
{
    T result = default(T);
    int index = reader.GetOrdinal(fieldName);

    if (reader.IsDBNull(index))
    {
        return default(T);
    }

    if (typeof(T) == typeof(string))
    {
        result = (T)Convert.ChangeType(reader.GetString(index), typeof(T));
    }

    if (typeof(T) == typeof(int))
    {
        result = (T)Convert.ChangeType(reader.GetInt32(index), typeof(T));
    }

    if (typeof(T) == typeof(DateTime))
    {
        result = (T)Convert.ChangeType(reader.GetDateTime(index), typeof(T));
    }

    if (typeof(T) == typeof(byte[]))
    {
        OracleLob blob = reader.GetOracleLob(index);
        result = (T)Convert.ChangeType(blob.Value, typeof(T));
    }

    return result;
}

And you can use like string title = reader.GetValue<string>("title")

你可以使用像 string title = reader.GetValue<string>("title")

回答by Tim Medora

There are clearly-defined mappingsfor CLR and SQL types, so the question is really how to efficiently and accurately map those types. Long-term, the easiest way is probably to use an automated mapping process which maps the properties of your class to the columns in the DataRow. You can write your own or find many examples/products online (any ORM features this as core functionality).

CLR 和 SQL 类型有明确定义的映射,所以问题实际上是如何有效和准确地映射这些类型。从长远来看,最简单的方法可能是使用自动映射过程,将类的属性映射到DataRow. 您可以自己编写或在线查找许多示例/产品(任何 ORM 都将此作为核心功能)。

Assuming that you still want to make manual assignments, you need to determine how you want to handle null values from the database. Do you want to assign them to a corresponding nullable type? do you want to use default(T)? do you want to use another value (default can be a poor substitute for null)? For example, a temperature of 0 degrees is perfectly valid, but default(float) == 0. If you use default(T), you might not be able to tell the difference between zero and a value that was null in the database.

假设您仍然希望进行手动分配,您需要确定您希望如何处理来自数据库的空值。您想将它们分配给相应的可为空类型吗?你想用default(T)吗?你想使用另一个值吗(默认值可能是空值的糟糕替代品)?例如,0 度的温度是完全有效的,但是default(float) == 0. 如果使用default(T),您可能无法区分零和数据库中的空值之间的区别。

Once you have your assignment strategy defined, put the code into a reusable form (extension methods, helper class, etc.) Prefer unboxing to the exact type when possible, as this will be the fastest. Following that, unbox to type, then cast. Following that, use the Convertclass.

定义分配策略后,将代码放入可重用的形式(扩展方法、帮助程序类等)。如果可能,最好将取消装箱到确切的类型,因为这将是最快的。之后,拆箱打字,然后投射。之后,使用Convert该类。