Clicky

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();                             
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 

Select allOpen in new window


asked 12/13/2011 11:22

mcs26's gravatar image

mcs26 ♦♦


4 Answers:
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:

1:
2:
3:
4:
DataTable dt = new DataTable();

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

answered

kaufmed's gravatar image

kaufmed

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

answered 2011-12-14 at 07:30:18

mcs26's gravatar image

mcs26

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.

1:
2:
3:
4:
5:
6:
7:
8:
9:
if (PriceReader.HasRows)
    {
        int rowCount = 0;

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

...
link

answered 2011-12-14 at 07:34:01

kaufmed's gravatar image

kaufmed

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.
link

answered 2011-12-14 at 07:38:36

kaufmed's gravatar image

kaufmed

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×59
×5
×156
×14
×6

Asked: 12/13/2011 11:22

Seen: 218 times

Last updated: 12/14/2011 06:08