I am retrieving columns of different types and checking for null before assigning the class's corresponding property. For string column Its all good. However, I need to decide what to do for the DateTime, Bool and the Enum type?
a) Do I should use nullable DateTime property for Class A or there is a better practice?
b) Is the checking for enum and the bool correct in the below code or there is a better way fo doing this?
public static List<ClassA> Select(string connectionString)
{
List<ClassA> ClassAList = new List<ClassA>();
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand command = new SqlCommand(SPROC_ClassA_Select, con))
{
using (SqlDataReader reader = command.ExecuteReader())
{
int MyGuidOrdinal = reader.GetOrdinal("MyGuid") ;
int MyStringOrdinal = reader.GetOrdinal("MyString") ;
int MyDateTimeOrdinal = reader.GetOrdinal("MyDateTime") ;
int MyBooleanOrdinal = reader.GetOrdinal("MyBoolean") ;
int MyEnumValueOrdinal = reader.GetOrdinal("MyEnumValue") ;
if(reader.HasRows)
{
while (reader.Read())
{
ClassA classA = new ClassA
{
MyGuid = reader.GetGuid(MyGuidOrdinal),
MyString = reader["MyString"] is DBNull ? null : reader.GetString(MyStringOrdinal),
MyDateTime = reader["MyDateTime"] is DBNull ? DateTime.MinValue : reader.GetDateTime(MyDateTimeOrdinal),
MyBoolean = reader["MyBoolean"] is DBNull ? false : reader.GetBoolean(MyBooleanOrdinal),
MyEnumValue = reader["MyEnumValue"] is DBNull ? (int)MyEnumValue.Value1 : reader.GetInt32(MyEnumValueOrdinal),
};
ClassAList.Add(classA);
}
}
return ClassAList;
}
}
}
And below is the enum:-
public enum MyEnumValue
{
value1 =1,
value2
}