DbDataReader: Matching fields to order or occurrence?
-
I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr["OrderId"];
}
}But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks
-
I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr["OrderId"];
}
}But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks
You can try the other overload[^] of the
DbDataReader's Item[]
property:using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr[1];
}
} -
I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr["OrderId"];
}
}But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks
dr.GetOrdinal("fieldName")
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost
-
I have to read an excel file and ensure the columns are in their correct order (the file is created manually and sometimes, columns are inserted in the incorrect order). I'm reading the file via microsoft jet oledb 4.0. I then insert the values into a DbDataReader. I now have to check to see if each column is in it's propper position. So, as of now, I have the following to start with (The data reader field names are based on the column header names of the excel file):
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
String col1 = dr["OrderId"];
}
}But my question is, how do I ensure dr["OrderId"] is the 1st field within the dbDataReader? Thanks
There should never be any reason to insist that the columns are in a particular order. You're doing something wrong.
-
There should never be any reason to insist that the columns are in a particular order. You're doing something wrong.
+5, since the order of the columns in a record could be coming from the server in a random order. And life is soo much easier if you just decide the order in the Presentation Layer - preferably having the user choose one. OTOH, this is an Excel-sheet, and it might come without headers. In that case, only the position of the column would indicate what cell (dbfield) you're actually editing. Things become harder if the user is allowed to move those things around. In an ideal world, each row would have a "caption" on top of the file.
Bastard Programmer from Hell :suss:
-
+5, since the order of the columns in a record could be coming from the server in a random order. And life is soo much easier if you just decide the order in the Presentation Layer - preferably having the user choose one. OTOH, this is an Excel-sheet, and it might come without headers. In that case, only the position of the column would indicate what cell (dbfield) you're actually editing. Things become harder if the user is allowed to move those things around. In an ideal world, each row would have a "caption" on top of the file.
Bastard Programmer from Hell :suss:
Eddy Vluggen wrote:
it might come without headers
Then you're hosed. :-D