如何检查从存储过程返回的NULL

本文关键字:存储过程 返回 NULL 何检查 检查 | 更新日期: 2023-09-27 17:58:15

我需要检查我的存储过程是否返回了null值。目前我有以下代码:

public static bool MyMethod(String isoNum)
{
    SqlConnection conn = null;
    bool regionExists = true;
    try
    {
        using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["String"].ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand("SPU_GetRegionBasedOnIso", conn))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@isoNum", isoNum);
                using (SqlDataReader dr = cmd.ExecuteReader())
                {
                **<<Here I need to check for NULL value>>** 
                }
            }
        }
    }
    catch (Exception e)
    {
        throw new System.Exception(e.Message.ToString());
    }
    return regionExists;
}

在这里检查NULL的最佳方法是什么?

感谢

如何检查从存储过程返回的NULL

使用ExecuteScalar而不是ExecuteReader,并与DBNull.Value进行比较。

object result = cmd.ExecuteScalar();
if (result == DBNull.Value)
{
    ....
}