IDataReader - IsDBNull
An IDataReader extension method that query if '@this' is database null.
public static void Main() { const string sql = @"SELECT 1, NULL"; using (var conn = new SqlConnection(FiddleHelper.GetConnectionStringSqlServer())) { conn.Open(); using (DbCommand command = conn.CreateCommand()) { command.CommandText = sql; using (IDataReader reader = command.ExecuteReader()) { while (reader.Read()) { //C# Extension Method: IDataReader - IsDBNull bool result1 = reader.IsDBNull(0); // return false; bool result2 = reader.IsDBNull(1); // return true; Console.WriteLine(result1); Console.WriteLine(result2); } } } } }
View Source
using System.Data; public static partial class Extensions { /// <summary> /// An IDataReader extension method that query if '@this' is database null. /// </summary> /// <param name="this">The @this to act on.</param> /// <param name="name">The name.</param> /// <returns>true if database null, false if not.</returns> public static bool IsDBNull(this IDataReader @this, string name) { return @this.IsDBNull(@this.GetOrdinal(name)); } }