how to check if a datareader is null or empty

前端 未结 12 1779
谎友^
谎友^ 2020-12-24 01:52

I have a datareader that return a lsit of records from a sql server database. I have a field in the database called "Additional". This field is 50% of the time emp

12条回答
  •  清歌不尽
    2020-12-24 02:36

    I haven't used DataReaders for 3+ years, so I wanted to confirm my memory and found this. Anyway, for anyone who happens upon this post like I did and wants a method to test IsDBNull using the column name instead of ordinal number, and you are using VS 2008+ (& .NET 3.5 I think), you can write an extension method so that you can pass the column name in:

    public static class DataReaderExtensions
    {
        public static bool IsDBNull( this IDataReader dataReader, string columnName )
        {
            return dataReader[columnName] == DBNull.Value;
        }
    }
    

    Kevin

提交回复
热议问题