Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static int? GetNullableInt32(this IDataRecord dr, int ordinal)
- {
- int? nullInt = null;
- return dr.IsDBNull(ordinal) ? nullInt : dr.GetInt32(ordinal);
- }
- public static int? GetNullableInt32(this IDataRecord dr, string fieldname)
- {
- int ordinal = dr.GetOrdinal(fieldname);
- return dr.GetNullableInt32(ordinal);
- }
- public static Nullable<T> GetNullable<T>(this IDataRecord dr, int ordinal)
- {
- Nullable<T> nullValue = null;
- return dr.IsDBNull(ordinal) ? nullValue : (Nullable<T>) dr.GetValue(ordinal);
- }
- public static T GetNullable<T>(this IDataRecord dr, int ordinal)
- {
- return dr.IsDBNull(ordinal) ? default(T) : (T) dr.GetValue(ordinal);
- }
- public static T Get<T>( this IDataRecord dr, int ordinal)
- {
- T nullValue = default(T);
- return dr.IsDBNull(ordinal) ? nullValue : (T) dr.GetValue(ordinal);
- }
- public void Code(params string[] args)
- {
- IDataRecord dr= null;
- int? a = Get<int?>(dr, 1);
- string b = Get<string>(dr, 2);
- }
- private void Test()
- {
- int? iTestA;
- int? iTestB;
- int iTestC;
- string sTestA;
- string sTestB;
- //Create connection
- using (SqlConnection oConnection = new SqlConnection(@""))
- {
- //Open connection
- oConnection.Open();
- //Create command
- using (SqlCommand oCommand = oConnection.CreateCommand())
- {
- //Set command text
- oCommand.CommandText = "SELECT null, 1, null, null, '1'";
- //Create reader
- using (SqlDataReader oReader = oCommand.ExecuteReader())
- {
- //Read the data
- oReader.Read();
- //Set the values
- iTestA = oReader[0] as int?;
- iTestB = oReader[1] as int?;
- iTestC = oReader[2] as int? ?? -1;
- sTestA = oReader[3] as string;
- sTestB = oReader[4] as string;
- }
- }
- }
- }
- public static T GetData<T>(this IDataReader reader, Func<int, T> getFunc, int index)
- {
- if (!reader.IsClosed)
- {
- return getFunc(index);
- }
- throw new ArgumentException("Reader is closed.", "reader");
- }
- public static T GetDataNullableRef<T>(this IDataReader reader, Func<int, T> getFunc, int index) where T : class
- {
- if (!reader.IsClosed)
- {
- return reader.IsDBNull(index) ? null : getFunc(index);
- }
- throw new ArgumentException("Reader is closed.", "reader");
- }
- public static T? GetDataNullableValue<T>(this IDataReader reader, Func<int, T> getFunc, int index) where T : struct
- {
- if (!reader.IsClosed)
- {
- return reader.IsDBNull(index) ? (T?)null : getFunc(index);
- }
- throw new ArgumentException("Reader is closed.", "reader");
- }
- private static Whatever CreateObject(IDataReader reader)
- {
- Int32? id = reader.GetDataNullableValue<Int32>(reader.GetInt32, 0);
- string name = reader.GetDataNullableRef<string>(reader.GetString, 1);
- Int32 x = reader.GetData<Int32>(reader.GetInt32, 2);
- }
- public static T Get<T>(this IDataRecord rec, Func<int, T> GetValue, int ordinal)
- {
- return rec.IsDBNull(ordinal) ? default(T) : GetValue(ordinal);
- }
- public static T Get<T>(this IDataRecord rec, Func<IDataRecord, int, T> GetValue, int ordinal)
- {
- return rec.IsDBNull(ordinal) ? default(T) : GetValue(rec, ordinal);
- }
- public static Func<IDataRecord, int, int> GetInt32 = (rec, i) => rec.GetInt32(i);
- public static Func<IDataRecord, int, bool> GetBool = (rec, i) => rec.GetBoolean(i);
- public static Func<IDataRecord, int, string> GetString = (rec, i) => rec.GetString(i);
- rec.Get(GetString, index);
- rec.Get(GetInt32, index);
- DataRow record = GetSomeRecord();
- int? someNumber = record[15] as int?
- Guid? someUID = record["MyPrimaryKey"] as Guid?;
- string someText = GetSomeText();
- record["Description"] = someText.ToDbString();
- // ........
- public static class StringExtensionHelper {
- public static object ToDbString( this string text ) {
- object ret = null != text ? text : DBNull.Value
- return ret;
- }
- }
- public static class StringExtensionHelper {
- public static object ToDbValue( this object value ) {
- object ret = object.ReferenceEquals( value, null ) ? (object)DBNull.Value : value;
- return ret;
- }
- }
Add Comment
Please, Sign In to add comment