Link to home
Start Free TrialLog in
Avatar of mcs26
mcs26

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcs26
mcs26

ASKER

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,
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

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.