Tycho.Sql - SqlReader Object

The SqlReader object is a Tycho.Sql abstraction of the standard SqlDataReader object provided by ADO.NET. It offers convenient methods for retrieval of Sql data values into C# native data types.

Note that all methods that return value types return them as nullable value types to account for the possibility of null values in Sql.

Also Note that conversions of data types between Sql and your application may throw exceptions, for example converting a large value numeric Sql value to an Int16 may very well throw an OverFlowException. Tycho.Sql makes no assumptions about your data or code. If you ask it to do something that doesn't make sense, an Exception will be thrown. Only you know your data.

Also Also Note - This SqlReader supplied by Tycho.Sql attempts to address the most common developer needs, however it is functionally impossible to predict all use cases here. When things get too complex for SqlReader to handle, you have direct access to the ADO.NET underlying SqlDataReader object by accessing the DataReader property.

Methods - Strings

GetString - Get a String value from Sql.

string GetString(string columnName, string format = null)

When the Sql native data type does not map easily to a "string", Tycho.Sql will attempt to convert it using the optional format parameter as a string formatter.

Methods - Numerics

GetInt64 - Get an Int64 value from Sql.

long? GetInt64(string columnName)

Most Sql native numeric data types can be converted to an Int64. If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as an Int64 returning null on failure.

GetInt32 - Get an Int32 value from Sql.

int? GetInt32(string columnName)

Most Sql native numeric data types can be converted to an Int32. If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as an Int32 returning null on failure.

GetInt16 - Get an Int16 value from Sql.

short? GetInt16(string columnName)

Most Sql native numeric data types can be converted to an Int16. If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as an Int16 returning null on failure.

GetDouble - Get a Double value from Sql.

double? GetDouble(string columnName)

Most Sql native numeric data types can be converted to a Double. If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as a Double returning null on failure.

GetFloat - Get a Float value from Sql.

float? GetFloat(string columnName)

Most Sql native numeric data types can be converted to a Float. If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as a Float returning null on failure.

Methods - Dates & Times

GetDateTime - Get a DateTime value from Sql.

DateTime? GetDateTime(string columnName)

If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as a DateTime returning null on failure.

GetDateTimeOffset - Get a DateTimeOffset value from Sql.

DateTimeOffset? GetDateTimeOffset(string columnName)

If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as a DateTimeOffset returning null on failure.

GetTimeSpan - Get a TimeSpan value from Sql.

TimeSpan? GetTimeSpan(string columnName)

If the Sql native data type is a string equivalent, Tycho.Sql will make an attempt to parse it as a TimeSpan returning null on failure.

Methods - Misc

GetBoolean - Get a Boolean value from Sql.

bool? GetBoolean(string columnName)

Native Sql String and Numeric data types can be read as Boolean values. String values are first converted to Int64, then treated as a numeric. Numerics evaluate to boolean false only if they equal 0, and to boolean true for all other values.

GetGuid - Get a Guid value from Sql.

Guid? GetGuid(string columnName)

GetEnum - Get an Enumeration value from Sql.

T GetEnum(string columnName, T defaultValue, bool ignoreCase = true)

Native Sql String and Integer data types can be read directly as generic Enum values. In the case of strings, an attempt is made to parse it as the specified Enumeration. In the case of Integers, the value is simply cast as the specified Enum type. A defaultValue is required in case Sql returns a null value.

GetXmlDocument - Get an XmlDocument from Sql.

XmlDocument GetXmlDocument(string columnName)

Sql Native Xml and String data types can be extracted directly as XmlDocument objects. If the source value is a string type which does not parse as a valid XmlDocument, null is returned.

GetXDocument - Get an XDocument from Sql.

XDocument GetXDocument(string columnName)

Sql Native Xml and String data types can be extracted directly as XDocument objects. If the source value is a string type which does not parse as a valid XDocument, null is returned.

GetByteArray - Get a Byte Array from Sql.

byte[] GetByteArray(string columnName)

Sql Native Binary, Image, UDT, and Variant values can be extracted as simple byte arrays.