ws11
asked on
DbDataReader drops results
I have a scenario were my data clears out after connecting to an excel spreadsheet using DbDataReader. Below is the sample code. So if I mouse over datareader to the results view I see records at first but then disappears if I pause and does not return any records in the loop.
string Conn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=excelFile;Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(Conn))
{
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [sheet$]", conn);
conn.Open();
using (DbDataReader datareader = cmd.ExecuteReader())
{
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It will not loop it just goes through what the loop contains one time despite how many records I have. I have used this before but not with an excel file.
ASKER
If I stop my trace at using (DbDataReader datareader = cmd.ExecuteReader()) the results view shows my column count and all my records so it has the data but if I wait go back and look again it will clear the ienumerable.
ASKER
See images. Image1 is first view then Image2 is after. The ienumerable is clearing its self. But it should not do this when it loops until the loop is finish. At least that is what I would expect.
Image1.jpg
Image2.jpg
Image1.jpg
Image2.jpg
ASKER
So I guess something like this would continue to loop...
foreach (DbDataRecord record in reader)
foreach (DbDataRecord record in reader)
to loop through the records you will need to use Read method like shown here:
while(datareader.Read())
{
//get values..
}
Check this: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx
while(datareader.Read())
{
//get values..
}
Check this: http://www.csharp-station.com/Tutorials/AdoDotNet/Lesson04.aspx
ASKER
Sorry I did not follow your initial post but figured it out that I needed to go row by row. I thought that was what ienumerables enumeration was all about. Any comments on the enumeration aspect would be greatly appreciated.
ASKER
Yes I have that already sorry for not posting that part.
Where is your loop...or code you are trying to read data from?