CSharp reading data from Access Database using oledbdatareader

Hi,

I am writing some in code in C# which retrieves data from an Access database (please see it below). According to some of the articles on the internet there is no count property for oledbdatareader. I know you can query the number & then run the query again but I do not wish to do this as the query takes a while already without running it twice. Is there a better way to query data from an access database (or SQL Server going forward) than how I have done it below & where I am also able to count the number records returned.

Thanks,

OleDbConnection dbConnection = new OleDbConnection(cn + dbPath + "dbFX_1Min_db.mdb");
            dbConnection.Open();

            OleDbCommand SQLQuery = dbConnection.CreateCommand();

            SQLQuery.CommandText= @"SELECT [Plot_PriceData].[DTime], [Plot_PriceData].[Close], [Plot_PriceData].[Signal]
                                    FROM [Plot_PriceData]
                                    WHERE ((([Plot_PriceData].[Pair])='" + Security + @"') 
                                    AND (([Plot_PriceData].[DTime])>=#" + dtFrom + @"# 
                                    And ([Plot_PriceData].[DTime])<=#" + dtTo + @"#))
                                    ORDER BY [Plot_PriceData].[DTime];";
            
            OleDbDataReader PriceReader = SQLQuery.ExecuteReader();

            if (PriceReader.HasRows)
            {
                while (PriceReader.Read())
                {
                    Element priceElement = new Element();

                    priceElement.XDateTime = (DateTime)PriceReader["DTime"];
                    priceElement.YValue = (Double)PriceReader["Close"];
                    

                        if ((string)PriceReader["Signal"]=="B")
                        {
                            priceElement.Annotation = new Annotation();
                            priceElement.Annotation.Label.Text="B";
                            priceElement.SmartLabel.Alignment = LabelAlignment.Bottom;
                        }
                        else if((string)PriceReader["Signal"] =="S")
                        {
                            priceElement.Annotation = new Annotation();                            
                            priceElement.Annotation.Label.Text="S";
                            priceElement.SmartLabel.Alignment = LabelAlignment.Top;
                        }

                    PriceSeries.Elements.Add(priceElement);
                }
            }

            PriceReader.Close();
            dbConnection.Close();

Open in new window

mcs26Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
If the data being returned is not terribly large, you can load it into a DataTable. Then you would have access to a Count property on the rows:

DataTable dt = new DataTable();

dt.Load(PriceReader);
int rowCount = dt.Rows.Count;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mcs26Author Commented:
Hi Kaufmed,

More often than not the number of rows will be 100,000 with 11 fields although pontentially it could also be 1million. Would that be ok to use a DataTable?

If it is to large for a DataTable what would you adivse?

Cheers,
käµfm³d 👽Commented:
I believe the 100,000 would be OK, but the 1,000,000, maybe not.

On second thought, can you not have a counter variable defined outside of the loop? For each call to Read, increment the variable.

if (PriceReader.HasRows)
    {
        int rowCount = 0;

        while (PriceReader.Read())
        {
            rowCount++;

...

Open in new window

käµfm³d 👽Commented:
Side note:  Your call to PriceReader.HasRows in this case is redundant. If you call PriceReader.Read the first time and it returns false, then there are no rows.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.