SQL Data Reader - handling Null column values

前端 未结 27 2192
甜味超标
甜味超标 2020-11-22 08:53

I\'m using a SQLdatareader to build POCOs from a database. The code works except when it encounters a null value in the database. For example, if the FirstName column in the

相关标签:
27条回答
  • 2020-11-22 08:58

    This Solution is less vendor-dependent and works with an SQL, OleDB, and MySQL Reader:

    public static string GetStringSafe(this IDataReader reader, int colIndex)
    {
        return GetStringSafe(reader, colIndex, string.Empty);
    }
    
    public static string GetStringSafe(this IDataReader reader, int colIndex, string defaultValue)
    {
        if (!reader.IsDBNull(colIndex))
            return reader.GetString(colIndex);
        else
            return defaultValue;
    }
    
    public static string GetStringSafe(this IDataReader reader, string indexName)
    {
        return GetStringSafe(reader, reader.GetOrdinal(indexName));
    }
    
    public static string GetStringSafe(this IDataReader reader, string indexName, string defaultValue)
    {
        return GetStringSafe(reader, reader.GetOrdinal(indexName), defaultValue);
    }
    
    0 讨论(0)
  • 2020-11-22 08:58

    What I tend to do is replace the null values in the SELECT statement with something appropriate.

    SELECT ISNULL(firstname, '') FROM people
    

    Here I replace every null with a blank string. Your code won't throw in error in that case.

    0 讨论(0)
  • 2020-11-22 08:58

    This method is dependent on indexFirstName which should be the zero-based column ordinal.

    if(!sqlReader.IsDBNull(indexFirstName))
    {
      employee.FirstName = sqlreader.GetString(indexFirstName);
    }
    

    If you don't know the column index but wan't to check a name you can use this extension method instead:

    public static class DataRecordExtensions
    {
        public static bool HasColumn(this IDataRecord dr, string columnName)
        {
            for (int i=0; i < dr.FieldCount; i++)
            {
                if (dr.GetName(i).Equals(columnName, StringComparison.InvariantCultureIgnoreCase))
                    return true;
            }
            return false;
        }
    }
    

    And use the method like this:

    if(sqlReader.HasColumn("FirstName"))
    {
      employee.FirstName = sqlreader["FirstName"];
    }
    
    0 讨论(0)
  • 2020-11-22 09:00

    You need to check for IsDBNull:

    if(!SqlReader.IsDBNull(indexFirstName))
    {
      employee.FirstName = sqlreader.GetString(indexFirstName);
    }
    

    That's your only reliable way to detect and handle this situation.

    I wrapped those things into extension methods and tend to return a default value if the column is indeed null:

    public static string SafeGetString(this SqlDataReader reader, int colIndex)
    {
       if(!reader.IsDBNull(colIndex))
           return reader.GetString(colIndex);
       return string.Empty;
    }
    

    Now you can call it like this:

    employee.FirstName = SqlReader.SafeGetString(indexFirstName);
    

    and you'll never have to worry about an exception or a null value again.

    0 讨论(0)
  • 2020-11-22 09:01
    private static void Render(IList<ListData> list, IDataReader reader)
            {
                while (reader.Read())
                {
    
                    listData.DownUrl = (reader.GetSchemaTable().Columns["DownUrl"] != null) ? Convert.ToString(reader["DownUrl"]) : null;
                    //没有这一列时,让其等于null
                    list.Add(listData);
                }
                reader.Close();
            }
    
    0 讨论(0)
  • 2020-11-22 09:03

    Check sqlreader.IsDBNull(indexFirstName) before you try to read it.

    0 讨论(0)
提交回复
热议问题