如何在c#中检查null条件 [英] How to check null condition in c#

查看:84
本文介绍了如何在c#中检查null条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些代码(如下所示)来获取我的C#应用​​程序的数据库结果...

 SqlCommand dbCommand =  new  SqlCommand(); 
dbCommand.CommandText = SElECT MAX(PVRID)FROM PVR;
int maxPVRNo = Convert.ToInt32(dbCommand.ExecuteScalar());

if (maxPVRNo == 1
// 做某事
if (maxPVRNo == 0
// 做点什么





当PVR数据表没有记录时它会返回空值。在这种情况下我的转换代码失败,因为null值无法转换为整数值...



所以我想知道,如何检查这种类型的空值情况(当ExecuteScalar()返回空值时)

解决方案

如果你真的想测试null,不要将值作为一个返回INT。默认情况下,int不是可空类型,但是对象是 - 所以,将值作为对象获取并测试:

  object  nullableValue = dbCommand.ExecuteScalar(); 
int myValue;
if (nullableValue == null || nullableValue == DBNull.Value)
{
myValue = 0 ;
}
else
{
int .TryParse (nullableValue, out myValue);
}


如果你可以改变查询,Pete的解决方案是最好的。如果你不能,那么你需要添加一些额外的测试。



VB有一个非常有用的功能, IsNumeric 。即使您使用的是C#,您仍然可以通过添加对 Microsoft.VisualBasic 的引用来访问该方法。 (可能有一个C#等价物,但我不知道它是什么。嘿,.Net方法是.Net方法。)所以你可能想试试这个:

 SqlCommand dbCommand =  new  SqlCommand(); 
dbCommand.CommandText = SElECT MAX(PVRID)FROM PVR;

object result = dbCommand.ExecuteScalar();

if (Microsoft.VisualBasic.Information.IsNumeric(result))
{
int maxPVRNo = Convert.ToInt32(result);
if (maxPVRNo == 0
{
// 一件事
}
if (maxPVRNo == 1
{
// < span class =code-comment>另一件事
}
}
其他
{
// 没有东西
}

}



你在这里做的是在确定结果实际上是一个数字后转换为整数。 MAX 返回的唯一非数字值是 null ,因此您也可以捕获它。


有一种方法可以将其转为头部,而不需要测试null。将命令文本更改为

   SELECT COALESCE(MAX(PVRID),0)FROM PVR ; 

如果MAX(PVRID)中的值为空,则此代码将返回0.


I have written some code(shown in below) to get a database result to my C# application...

SqlCommand dbCommand = new SqlCommand();
      dbCommand.CommandText = "SElECT MAX(PVRID) FROM PVR";
      int maxPVRNo = Convert.ToInt32(dbCommand.ExecuteScalar());

      if(maxPVRNo == 1)
          //do something
      if(maxPVRNo ==0)
          //do something



when the PVR data table have no records it will return the null value.In this case my conversion code is fail because null value cannot be convert to integer value...

So i want to know, how to check null value in this kind of situation ( When the ExecuteScalar() return a null value )

解决方案

If you really want to test for null, don't return the value as an int. By default, ints aren't nullable types, but objects are - so, get the value as an object and test that:

object nullableValue = dbCommand.ExecuteScalar();
int myValue;
if (nullableValue == null || nullableValue == DBNull.Value)
{
  myValue = 0;
}
else
{
  int.TryParse(nullableValue, out myValue);
}


If you can alter the query, Pete's solution is the best. If you cannot, then you will need to add some additional tests.

VB has a very useful function, IsNumeric. Even though you are using C#, you can still access the method by adding a reference to Microsoft.VisualBasic. (There is probably a C# equivalent, but I don't know what it is. Hey, a .Net method is a .Net method.) So you might want to try this:

    SqlCommand dbCommand = new SqlCommand();
    dbCommand.CommandText = "SElECT MAX(PVRID) FROM PVR";

    object result = dbCommand.ExecuteScalar();

    if (Microsoft.VisualBasic.Information.IsNumeric(result))
    {
        int maxPVRNo = Convert.ToInt32(result);
        if (maxPVRNo == 0)
        {
            // one thing
        }
        if (maxPVRNo == 1)
        {
            // another thing
        }
    }
    else
    {
        // no thing
    }

}


What you are doing here is making the conversion to an integer after you have determined that the result is, in fact, a number. The only non-numeric value that MAX would return is null, so you can catch that as well.


There's a way to turn this on the head, and not need to test for null. Change your command text to

"SELECT COALESCE(MAX(PVRID), 0) FROM PVR";

This code will return 0 if the value in MAX(PVRID) is null.


这篇关于如何在c#中检查null条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆