c#,nullable,data,reader

Kerim 8/7/2016 1people says helpful

C# 2.0 Nullable Data Readers This code shows nullable data readers for .NET 2.0 nullable types.

C#
 person.Age = reader.GetInt32(reader.GetOrdinal("Age")); 
person.Age = reader.GetInt32("Age");
person.Age = (int)row["Age"];
person.Age = Convert.ToInt32(row["Age"]);

if (reader.IsDBNull(reader.GetOrdinal("FiredDate"))) person.FiredDate = null; else person.FiredDate = reader.GetDateTime(reader.GetOrdinal("FiredDate"));

public interface INullableReader { bool GetBoolean(string name); Nullable<bool> GetNullableBoolean(string name); byte GetByte(string name); Nullable<byte> GetNullableByte(string name); char GetChar(string name); Nullable<char> GetNullableChar(string name); DateTime GetDateTime(string name); Nullable<DateTime> GetNullableDateTime(string name); decimal GetDecimal(string name); Nullable<Decimal> GetNullableDecimal(string name); double GetDouble(string name); Nullable<double> GetNullableDouble(string name); float GetFloat(string name); Nullable<float> GetNullableFloat(string name); Guid GetGuid(string name); Nullable<Guid> GetNullableGuid(string name); short GetInt16(string name); Nullable<short> GetNullableInt16(string name); int GetInt32(string name); Nullable<int> GetNullableInt32(string name); long GetInt64(string name); Nullable<long> GetNullableInt64(string name); string GetString(string name); string GetNullableString(string name); object GetValue(string name); bool IsDBNull(string name); }

dr = new NullableDataReader(db.ExecuteReader(cmd));

person.Age = dr.GetInt32("Age"); person.FiredDate = dr.GetNullableDateTime("FiredDate");

try { while (dr.Read()) { Person person = new Person(); person.Age = dr.GetInt32("Age"); person.FiredDate = dr.GetNullableDateTime("FiredDate"); personList.Add(person); } } finally { dr.Dispose(); }

NullableDataRowReader dr = new NullableDataRowReader(row); person.Age = dr.GetInt32("Age"); person.FiredDate = dr.GetNullableDateTime("FiredDate");

NullableDataRowReader dr = new NullableDataRowReader(); foreach (DataRow row in dataTable.Rows) { dr.Row = row; Person person = new Person(); person.Age = dr.GetInt32("Age"); person.FiredDate = dr.GetNullableDateTime("FiredDate"); personList.Add(person); }

NullableDataReader dr = new NullableDataReader(dataTable.CreateDataReader());


public Address BuildItem(INullableReader dr) { Address address = new Address(); address.ID = dr.GetInt32(Params.AddressID); address.StreetAddress1 = dr.GetString(Params.StreetAddress1); address.StreetAddress2 = dr.GetString(Params.StreetAddress2); address.City = dr.GetString(Params.City); address.State = dr.GetString(Params.State); address.ZipCode = dr.GetString(Params.ZipCode); return address; }

NullableDataReader dr = new NullableDataReader(db.ExecuteReader(cmd)); person.Address = addressMapper.BuildItem(dr);

NullableDataRowReader dr = new NullableDataRowReader(addressTable.Rows[0]); person.Address = addressMapper.BuildItem(dr);

public int GetInt32(int i){

return reader.GetInt32(i);

}

public int GetInt32(string name)

{ return reader.GetInt32(reader.GetOrdinal(name)); }

public Nullable<int> GetNullableInt32(string name) { return this.GetNullableInt32(reader.GetOrdinal(name)); } public Nullable<int> GetNullableInt32(int index) { Nullable<int> nullable; if (reader.IsDBNull(index)) { nullable = null; } else { nullable = GetInt32(index); } return nullable; }

private Nullable<T> GetNullable<T>(int ordinal, Conversion<T> convert) where T : struct { Nullable<T> nullable; if (reader.IsDBNull(ordinal)) { nullable = null; } else { nullable = convert(ordinal); } return nullable; }

private delegate T Conversion<T>(int ordinal);

public Nullable<INT> GetNullableInt32(int index) { return GetNullable<INT>(index, GetInt32); }

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments