Click here to Skip to main content
15,887,967 members
Home / Discussions / C#
   

C#

 
GeneralRe: Working with OleDb and Null Values Pin
Matt U.18-May-11 14:48
Matt U.18-May-11 14:48 
QuestionRe: Working with OleDb and Null Values Pin
Luc Pattyn18-May-11 16:47
sitebuilderLuc Pattyn18-May-11 16:47 
AnswerRe: Working with OleDb and Null Values [modified] Pin
PIEBALDconsult18-May-11 16:59
mvePIEBALDconsult18-May-11 16:59 
GeneralRe: Working with OleDb and Null Values Pin
Luc Pattyn18-May-11 17:11
sitebuilderLuc Pattyn18-May-11 17:11 
GeneralRe: Working with OleDb and Null Values Pin
PIEBALDconsult18-May-11 17:29
mvePIEBALDconsult18-May-11 17:29 
GeneralRe: Working with OleDb and Null Values Pin
GenJerDan19-May-11 11:13
GenJerDan19-May-11 11:13 
AnswerRe: Working with OleDb and Null Values Pin
Shameel19-May-11 1:42
professionalShameel19-May-11 1:42 
AnswerRe: Working with OleDb and Null Values Pin
Pete O'Hanlon19-May-11 2:09
mvePete O'Hanlon19-May-11 2:09 
Luc Pattyn wrote:
Would it also negatively affect performance?

This tends to be treated as a micro-optimisation that is completely useless. There is a simple way to get around this performance hit, which is to use the ordinal based value, but use GetOrdinal to retrieve the position of the ordinal based on the field name.

The thing is, you only need to use GetOrdinal once for each field you are interested in - which means that you can use the ordinal in the loop over the reader.

Effectively, your code becomes something like this (with apologies because I'm just typing this off the top of my head in the CP editor):
C#
int customerId = reader.GetOrdinal("CustomerID");
int customerName = reader.GetOrdinal("Name");
while (reader.Read())
{
  Customer cust = new Customer(GetValue(reader, customerId), GetValue(reader, customerName));
}

private T GetValue(SqlDataReader dr, int position)
{
  if (dr.IsDbNull(position))
    return default(T);
  return (T)dr[position];
}

Forgive your enemies - it messes with their heads


My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility


GeneralRe: Working with OleDb and Null Values Pin
Luc Pattyn19-May-11 3:11
sitebuilderLuc Pattyn19-May-11 3:11 
GeneralRe: Working with OleDb and Null Values Pin
Pete O'Hanlon19-May-11 3:31
mvePete O'Hanlon19-May-11 3:31 
QuestionCombo+GroupBox Pin
KORCARI18-May-11 9:35
KORCARI18-May-11 9:35 
AnswerRe: Combo+GroupBox Pin
Expert Coming18-May-11 10:38
Expert Coming18-May-11 10:38 
AnswerRe: Combo+GroupBox Pin
RobCroll19-May-11 6:19
RobCroll19-May-11 6:19 
QuestionCall a function of other form [modified] Pin
MiguelSanchezCuarental18-May-11 5:03
MiguelSanchezCuarental18-May-11 5:03 
AnswerRe: Call a function of other form Pin
Wayne Gaylard18-May-11 5:47
professionalWayne Gaylard18-May-11 5:47 
AnswerRe: Call a function of other form Pin
Icharus18-May-11 5:51
Icharus18-May-11 5:51 
AnswerRe: Call a function of other form Pin
DaveyM6918-May-11 9:30
professionalDaveyM6918-May-11 9:30 
AnswerRe: Call a function of other form Pin
RobCroll19-May-11 6:34
RobCroll19-May-11 6:34 
QuestionIHTMLDOCUMENT2 and using all property to modify the webpage Pin
Member 70956418-May-11 4:23
Member 70956418-May-11 4:23 
QuestionBuild Multiuser Network application Pin
hemant saliya18-May-11 1:12
hemant saliya18-May-11 1:12 
AnswerRe: Build Multiuser Network application Pin
Wayne Gaylard18-May-11 1:15
professionalWayne Gaylard18-May-11 1:15 
AnswerRe: Build Multiuser Network application Pin
Pete O'Hanlon18-May-11 1:24
mvePete O'Hanlon18-May-11 1:24 
GeneralRe: Build Multiuser Network application Pin
Luc Pattyn18-May-11 3:56
sitebuilderLuc Pattyn18-May-11 3:56 
AnswerRe: Build Multiuser Network application Pin
Keith Barrow18-May-11 2:24
professionalKeith Barrow18-May-11 2:24 
AnswerRe: Build Multiuser Network application Pin
PIEBALDconsult18-May-11 2:46
mvePIEBALDconsult18-May-11 2:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.