在VB.NET中检查Datareader和Connection对象NULL。

问题描述 投票:0回答:1

我想把下面的C#代码转换成VB.NET。

        if (dr != null)
            dr.Close();
        if (con != null && con.State == ConnectionState.Open)
            con.Close();
        dr = null;

在VB.NET中,我没有找到任何合适的方法或途径来检查OracleDatareader和OracleConnection对象是否为NULL。我如何在VB.NET中解释上述代码?我使用System.Data.OrcleClient命名空间来访问Orcale数据库的数据。

vb.net ado.net
1个回答
1
投票

在vb.net中 !=null 成为 IsNot Nothing&& 成为 andAlso. 我们需要使用AndAlso,才能够像第二条if语句中那样进行短路。最后,我们需要使用AndAlso来获得像第二个if语句中的短路。== 只是 =.

因此,最终的结果是:

        If dr IsNot Nothing Then
            dr.Close()
        ElseIf con IsNot Nothing AndAlso con.State = ConnectionState.Open then
            con.Close()
        End If
        dr = Nothing
© www.soinside.com 2019 - 2024. All rights reserved.