Created
November 8, 2018 17:10
-
-
Save matteopic/9bc6d4ef79b7afef3e6772577918a985 to your computer and use it in GitHub Desktop.
A wrapper for System.Data.SqlClient.SqlDataReader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ResultSet : IDisposable | |
{ | |
private SqlDataReader reader; | |
public ResultSet(SqlDataReader reader) | |
{ | |
this.reader = reader; | |
} | |
public bool Next() | |
{ | |
return reader.Read(); | |
} | |
public Boolean HasColumn(String name) | |
{ | |
try | |
{ | |
reader.GetOrdinal(name); | |
return true; | |
} | |
catch (IndexOutOfRangeException ex) | |
{ | |
return false; | |
} | |
} | |
public bool GetBoolean(String columnName, bool defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (Boolean)value; | |
} | |
public int GetInt(String columnName) | |
{ | |
return GetInt(columnName, 0); | |
} | |
public int GetInt(String columnName, int defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (int)Convert.ChangeType(value, typeof(int)); | |
} | |
public long GetLong(String columnName) | |
{ | |
return GetLong(columnName, 0L); | |
} | |
public long GetLong(String columnName, long defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (long)Convert.ChangeType(value, typeof(long)); | |
} | |
public double? OptDouble(string columnName) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? (double?)null : (double)Convert.ChangeType(value, typeof(double)); | |
} | |
public double GetDouble(string columnName) | |
{ | |
return GetDouble(columnName, 0D); | |
} | |
public double GetDouble(string columnName, double defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (double)Convert.ChangeType(value, typeof(double)); | |
} | |
public float GetFloat(string columnName) | |
{ | |
return GetFloat(columnName, default(float)); | |
} | |
public float GetFloat(string columnName, float defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (float)Convert.ChangeType(value, typeof(float)); | |
} | |
public string GetString(string columnName) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? null : (string)value; | |
} | |
public DateTime GetDateTime(string columnName) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? default(DateTime) : (DateTime)value; | |
} | |
public DateTime GetDateTime(string columnName, DateTime defaultValue) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? defaultValue : (DateTime)value; | |
} | |
public DateTime? OptDateTime(string columnName) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? (DateTime?)null : (DateTime)value; | |
} | |
public char? OptChar(string columnName) | |
{ | |
object value = reader[columnName]; | |
return value is DBNull ? (char?)null : ((string)value)[0]; | |
} | |
public void Dispose() | |
{ | |
reader.Dispose(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment