简体   繁体   English

C#Object null检查

[英]C# Object null check

I read my database using DataReader. 我使用DataReader读取了我的数据库。

and some row does not have fdate value. 并且某些行没有fdate值。

so when I Convert the null date to DateTime then Error occurs. 因此,当我将null日期转换为DateTime时,会发生错误。

How can I check the field empty or not? 如何查看该字段是否为空?

AdsCommand cmd = conn.CreateCommand();
cmd.CommandText = "select name,fdate from abc";

AdsDataReader reader = cmd.ExecuteReader();

DateTime flsdate = (reader["fdate"].Equals(null))? Convert.ToDateTime(reader["fdate"]) : DateTime.Today;

I tried with Equals, but it does not work. 我尝试使用Equals,但它不起作用。

anybody know how to check the null object to avoid convert error? 有谁知道如何检查null对象以避免转换错误?

Thank you! 谢谢!

As everyone pointed you how to solve the issue, I am trying to give you the info regarding what is the difference between NULL and DBNull. 当大家指出如何解决问题时,我试图向您提供有关NULL和DBNull之间区别的信息。

  • null and DBNull are different. null和DBNull是不同的。

  • null is not an instance of any type. null不是任何类型的实例。 DBNull is a singleton class with one instance: DBNull.Value . DBNull是一个包含一个实例的单例类: DBNull.Value

  • null represents an invalid reference where as DBNull.Value represents a nonexistent value in DB. null表示无效引用,其中DBNull.Value表示DB中不存在的值。

  • DBNull.Value is what db providers provide for a nonexistant value in a table. DBNull.Value是数据库提供程序为表中的不存在的值提供的内容。

With that background (reader["fdate"].Equals(null)) is not correct to use here. 使用该背景(reader["fdate"].Equals(null))在这里使用是不正确的。 You have to check it with DBNull.Value . 你必须用DBNull.Value检查它。 If it is of type DBNull , or if it is equal to DBNull.Value , then assign what ever value you like. 如果它是DBNull类型,或者它等于DBNull.Value ,那么分配你喜欢的任何值。

In a situation such as this I like to represent nullable database columns with either a reference type (string for varchar) or a Nullable wrapped value type (DateTime?). 在这种情况下,我喜欢使用引用类型(varchar的字符串)或Nullable包装的值类型(DateTime?)来表示可空的数据库列。 This way you're more accurately representing the database schema in your program. 这样,您可以更准确地表示程序中的数据库架构。

This also allows you to more cleanly write the conversion logic using the format: 这也允许您使用以下格式更清晰地编写转换逻辑:

DateTime? fdate = datareader["fdate"] as DateTime?;

This cast will fail in the event the datareader result is a DbNull and fdate will be set to default(DateTime?), which is null. 如果datareader结果是DbNull并且fdate将设置为默认值(DateTime?),则该转换将失败,该值为null。 At that point you can obtain your real desired value by checking whether the nullable type has a value or not (fdate.HasValue), and if not, using your default - DateTime.Today. 此时,您可以通过检查可空类型是否具有值(fdate.HasValue)来获取您想要的实际值,如果不是,则使用您的默认值 - DateTime.Today。

DateTime flsdate = reader["fdate"].Equals(DBNull.Value)
    ? Convert.ToDateTime(reader["fdate"])
    : DateTime.Today;

But it seems dangerous to default the date to Today . 但是将日期默认为Today似乎很危险。 I'd do this instead: 我会这样做:

DateTime? flsdate = reader["fdate"].Equals(DBNull.Value)
    ? Convert.ToDateTime(reader["fdate"])
    : (DateTime?)null;

Furthermore, if the underlying tpe of the fdate column is already DateTime, don't use System.Convert: 此外,如果fdate列的底层tpe已经是DateTime,请不要使用System.Convert:

DateTime? flsdate = reader["fdate"].Equals(DBNull.Value)
    ? (DateTime?)reader["fdate"])
    : null;

Try the following: 请尝试以下方法:

DateTime flsdate = reader["fdate"] != null && reader["fdate"] != System.DbNull.Value
    ? DateTime.ParseExact(reader["fdate"]) 
    : DateTime.Today;
DateTime flsdate = DateTime.Today;
if(reader["fdate"] != null)
    flsdate = Convert.ToDateTime(reader["fdate"])

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

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