DataReader best-practices?

Here is the way that I do it: Int32 ordinal = dataReader. GetOrdinal("ColumnName"); if (!dataReader. IsDBNull(ordinal)) yourString = dataReader.

GetString(ordinal) It is important to check for DBNull like I have shown above because if the field is null in the DataReader it will throw an exception when you try to retrieve it.

Here is the way that I do it: Int32 ordinal = dataReader. GetOrdinal("ColumnName"); if (!dataReader. IsDBNull(ordinal)) yourString = dataReader.

GetString(ordinal); It is important to check for DBNull like I have shown above because if the field is null in the DataReader it will throw an exception when you try to retrieve it.

Absolutely agreed. I'm asking more about the syntactical differences of getting the actual value, using the DataReaders Item functionality vs. using the provided DataReader. Get methods.

– AJ. Dec 6 '09 at 14:54 I always use, datareader"column1", much nicer to read. – Paul Creasey Dec 6 '09 at 15:00 2 @Paul - While it is much nicer to read you are risking many exceptions with this approach as you are making many assumptions about the DataReader (that the column exists, the data is not DBNull, etc.).

– Andrew Hare Dec 6 '09 at 15:03 1 Since the datareader"column" format returns an object and is not strongly-typed, I don't think you'll get an exception right then if the data is DbNull. You'll just get DbNull. Value cast to object.

Of course, you may get an exception later when you try to use the value if you don't first do a Convert. IsDbNull check. – Joel Mueller Dec 6 '09 at 17:26.

Operator. /// /// Returns an IEnumerable view of the data reader. /// WARNING: Does not support readers with multiple result sets.

/// The reader will be closed after the first result set is read. /// public static IEnumerable AsEnumerable(this IDataReader reader) { if (reader == null) throw new ArgumentNullException("reader"); using (reader) { while (reader.Read()) { yield return reader; } } } public static int? GetNullableInt32(this IDataRecord dr, string fieldName) { return GetNullableInt32(dr, dr.GetOrdinal(fieldName)); } public static int?

GetNullableInt32(this IDataRecord dr, int ordinal) { return dr.IsDBNull(ordinal)? Null : (int? )dr.GetInt32(ordinal); } ...and so on for the other GetDataType() methods on IDataReader.

You could simply call reader.Cast(). – SLaks? Dec 6 '09 at 22:04 Well, you could if IDataReader had anything at all to do with IEnumerable.

However, this is not the case. The signature is: public interface IDataReader : IDisposable, IDataRecord – Joel Mueller Dec 7 '09 at 17:33 Nice, but you might want to close the reader after the: while (reader.Read()) loop – Ulf Lindback Jul 1 '10 at 12:07 @Ulf - that's what the using (reader) statement is for. – Joel Mueller Jul 1 '10 at 18:13 Ah, of course (a bit new on C#)... – Ulf Lindback Jul 2 '10 at 9:08.

Do not use a DataSet object for scenarios where you can use a DataReader object. Use a DataReader if you need forward-only, read-only access to data and if you do not need to cache the data. Do not pass DataReader objects across physical server boundaries because they require open connections.

Use the DataSet when you need the added flexibility or when you need to cache data between requests. How to perform transactions in .

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions