1 //------------------------------------------------------------------------------
2 // <copyright file="DataRowExtenstions.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">spather</owner>
7 //------------------------------------------------------------------------------
9 using System.Data.DataSetExtensions;
11 namespace System.Data {
14 /// This static class defines the DataRow extension methods.
16 public static class DataRowExtensions {
19 /// This method provides access to the values in each of the columns in a given row.
20 /// This method makes casts unnecessary when accessing columns.
21 /// Additionally, Field supports nullable types and maps automatically between DBNull and
22 /// Nullable when the generic type is nullable.
24 /// <param name="row">
27 /// <param name="columnName">
28 /// The input column name specificy which row value to retrieve.
31 /// The DataRow value for the column specified.
33 public static T Field<T>(this DataRow row, string columnName) {
34 DataSetUtil.CheckArgumentNull(row, "row");
35 return UnboxT<T>.Unbox(row[columnName]);
39 /// This method provides access to the values in each of the columns in a given row.
40 /// This method makes casts unnecessary when accessing columns.
41 /// Additionally, Field supports nullable types and maps automatically between DBNull and
42 /// Nullable when the generic type is nullable.
44 /// <param name="row">
47 /// <param name="column">
48 /// The input DataColumn specificy which row value to retrieve.
51 /// The DataRow value for the column specified.
53 public static T Field<T>(this DataRow row, DataColumn column) {
54 DataSetUtil.CheckArgumentNull(row, "row");
55 return UnboxT<T>.Unbox(row[column]);
59 /// This method provides access to the values in each of the columns in a given row.
60 /// This method makes casts unnecessary when accessing columns.
61 /// Additionally, Field supports nullable types and maps automatically between DBNull and
62 /// Nullable when the generic type is nullable.
64 /// <param name="row">
67 /// <param name="columnIndex">
68 /// The input ordinal specificy which row value to retrieve.
71 /// The DataRow value for the column specified.
73 public static T Field<T>(this DataRow row, int columnIndex) {
74 DataSetUtil.CheckArgumentNull(row, "row");
75 return UnboxT<T>.Unbox(row[columnIndex]);
79 /// This method provides access to the values in each of the columns in a given row.
80 /// This method makes casts unnecessary when accessing columns.
81 /// Additionally, Field supports nullable types and maps automatically between DBNull and
82 /// Nullable when the generic type is nullable.
84 /// <param name="row">
87 /// <param name="columnIndex">
88 /// The input ordinal specificy which row value to retrieve.
90 /// <param name="version">
91 /// The DataRow version for which row value to retrieve.
94 /// The DataRow value for the column specified.
96 public static T Field<T>(this DataRow row, int columnIndex, DataRowVersion version) {
97 DataSetUtil.CheckArgumentNull(row, "row");
98 return UnboxT<T>.Unbox(row[columnIndex, version]);
102 /// This method provides access to the values in each of the columns in a given row.
103 /// This method makes casts unnecessary when accessing columns.
104 /// Additionally, Field supports nullable types and maps automatically between DBNull and
105 /// Nullable when the generic type is nullable.
107 /// <param name="row">
108 /// The input DataRow
110 /// <param name="columnName">
111 /// The input column name specificy which row value to retrieve.
113 /// <param name="version">
114 /// The DataRow version for which row value to retrieve.
117 /// The DataRow value for the column specified.
119 public static T Field<T>(this DataRow row, string columnName, DataRowVersion version) {
120 DataSetUtil.CheckArgumentNull(row, "row");
121 return UnboxT<T>.Unbox(row[columnName, version]);
125 /// This method provides access to the values in each of the columns in a given row.
126 /// This method makes casts unnecessary when accessing columns.
127 /// Additionally, Field supports nullable types and maps automatically between DBNull and
128 /// Nullable when the generic type is nullable.
130 /// <param name="row">
131 /// The input DataRow
133 /// <param name="column">
134 /// The input DataColumn specificy which row value to retrieve.
136 /// <param name="version">
137 /// The DataRow version for which row value to retrieve.
140 /// The DataRow value for the column specified.
142 public static T Field<T>(this DataRow row, DataColumn column, DataRowVersion version) {
143 DataSetUtil.CheckArgumentNull(row, "row");
144 return UnboxT<T>.Unbox(row[column, version]);
148 /// This method sets a new value for the specified column for the DataRow it
\92s called on.
150 /// <param name="row">
151 /// The input DataRow.
153 /// <param name="columnIndex">
154 /// The input ordinal specifying which row value to set.
156 /// <param name="value">
157 /// The new row value for the specified column.
159 public static void SetField<T>(this DataRow row, int columnIndex, T value) {
160 DataSetUtil.CheckArgumentNull(row, "row");
161 row[columnIndex] = (object)value ?? DBNull.Value;
165 /// This method sets a new value for the specified column for the DataRow it
\92s called on.
167 /// <param name="row">
168 /// The input DataRow.
170 /// <param name="columnName">
171 /// The input column name specificy which row value to retrieve.
173 /// <param name="value">
174 /// The new row value for the specified column.
176 public static void SetField<T>(this DataRow row, string columnName, T value) {
177 DataSetUtil.CheckArgumentNull(row, "row");
178 row[columnName] = (object)value ?? DBNull.Value;
182 /// This method sets a new value for the specified column for the DataRow it
\92s called on.
184 /// <param name="row">
185 /// The input DataRow.
187 /// <param name="column">
188 /// The input DataColumn specificy which row value to retrieve.
190 /// <param name="value">
191 /// The new row value for the specified column.
193 public static void SetField<T>(this DataRow row, DataColumn column, T value) {
194 DataSetUtil.CheckArgumentNull(row, "row");
195 row[column] = (object)value ?? DBNull.Value;
198 private static class UnboxT<T>
200 internal static readonly Converter<object, T> Unbox = Create(typeof(T));
202 private static Converter<object, T> Create(Type type)
204 if (type.IsValueType)
206 if (type.IsGenericType && !type.IsGenericTypeDefinition && (typeof(Nullable<>) == type.GetGenericTypeDefinition()))
208 return (Converter<object, T>)Delegate.CreateDelegate(
209 typeof(Converter<object, T>),
211 .GetMethod("NullableField", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic)
212 .MakeGenericMethod(type.GetGenericArguments()[0]));
216 return ReferenceField;
219 private static T ReferenceField(object value)
221 return ((DBNull.Value == value) ? default(T) : (T)value);
224 private static T ValueField(object value)
226 if (DBNull.Value == value)
228 throw DataSetUtil.InvalidCast(Strings.DataSetLinq_NonNullableCast(typeof(T).ToString()));
233 private static Nullable<TElem> NullableField<TElem>(object value) where TElem : struct
235 if (DBNull.Value == value)
237 return default(Nullable<TElem>);
239 return new Nullable<TElem>((TElem)value);