VB.Net“IsDBNull”的C#等价物是什么

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29369253/
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-09-17 18:56:25  来源:igfitidea点击:

What is the C# equivalent of VB.Net " IsDBNull"

c#vb.net

提问by Chandrasekhar Sahoo

In VB.Net you can write :

在 VB.Net 你可以这样写:

If Not IsDBNull(oCustomerNameDataRow(0)) Then
    cbCustomerName.Items.Add(oCustomerNameDataRow(0).ToString
End If

What is the equivalent of method IsDBNull in C#?

C# 中 IsDBNull 方法的等价物是什么?

回答by Дмитрий Чистик

if (!DBNull.Value.Equals(oCustomerNameDataRow[0]))
{
  //something
}

MSDN (DBNull.Value)

MSDN (DBNull.Value)

回答by Bj?rn-Roger Kringsj?

I would say the the equivalent of the IsDBNullmethod (Microsoft.VisualBasic.Information)located in the Microsoft.VisualBasicassembley

我会说等效于位于Microsoft.VisualBasicassembley中的IsDBNull方法(Microsoft.VisualBasic.Information)

Public Function IsDBNull(ByVal Expression As Object) As Boolean
    If Expression Is Nothing Then
        Return False
    ElseIf TypeOf Expression Is System.DBNull Then
        Return True
    Else
        Return False
    End If
End Function
Dim result As Boolean = IsDBNull(Nothing)

is the IsDBNullmethod (System.Convert)located in the mscorlibassembley:

是位于mscorlib汇编中的IsDBNull方法(System.Convert)

public static bool IsDBNull(object value) {
    if (value == System.DBNull.Value) return true;
    IConvertible convertible = value as IConvertible;
    return convertible != null? convertible.GetTypeCode() == TypeCode.DBNull: false;
}
bool result = System.Convert.IsDBNull(null);

回答by AminRostami

try this:

尝试这个:

create the Extension Method. follow this:

创建Extension Method. 按照这个:

public static bool IsDBNull(this object val)
{
     return Convert.IsDBNull(val);
}

and the use from this Extension Method.

以及由此而来的使用Extension Method

if(oCustomerNameDataRow[0].IsDBNull())
{
      // ...
}

I hope useful.

我希望有用。