none
RecordCount RRS feed

  • Question

  • Hi,

    I'm using the OLEDB object order to connect to database.

    How can I get the number of the columns which match to my query (RecordCount)?

     

    Thanks, Idan.

    Sunday, July 8, 2007 11:25 AM

Answers

  • Can you explain a bit more? Are you trying to find out how many records are being returned by a SELECT command? if this is the case there are a couple of ways.

     

    1) Use a DataTable to populate the results. The DataTable has a Rows property and within it a Count property. This will return you the number of rows in the datatable (number of records)

    2) you could also use a DataReader to loop through the records and then keep a number incremented in a while loop (while it has rows) to find out how many rows your query has returned.

     

    Example 1:

     



     
    OleDbCommand command = new OleDbCommand("CommandHere", ConnectionObject);
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
     
    DataTable table = new DataTable();
     
    command.Connection.Open();
    adapter.Fill(table);
    command.Connection.Close();
     
    MessageBox.Show("Results found: " + table.Rows.Count.ToString());
     

     

     

    Example 2:

     



    OleDbCommand command = new OleDbCommand("CommandHere", ConnectionObject);
    OleDbDataReader reader; 
    int rows = 0;
     
    command.Connection.Open();
    reader = command.ExecuteReader();
    while (reader.Read())
    {
       rows +=1;
    }
    command.Connection.Close();
     
    MessageBox.Show("Results found: " + rows.ToString());

     

     

    is this what you are after? Does this help?

    Sunday, July 8, 2007 12:17 PM

All replies

  • Can you explain a bit more? Are you trying to find out how many records are being returned by a SELECT command? if this is the case there are a couple of ways.

     

    1) Use a DataTable to populate the results. The DataTable has a Rows property and within it a Count property. This will return you the number of rows in the datatable (number of records)

    2) you could also use a DataReader to loop through the records and then keep a number incremented in a while loop (while it has rows) to find out how many rows your query has returned.

     

    Example 1:

     



     
    OleDbCommand command = new OleDbCommand("CommandHere", ConnectionObject);
    OleDbDataAdapter adapter = new OleDbDataAdapter(command);
     
    DataTable table = new DataTable();
     
    command.Connection.Open();
    adapter.Fill(table);
    command.Connection.Close();
     
    MessageBox.Show("Results found: " + table.Rows.Count.ToString());
     

     

     

    Example 2:

     



    OleDbCommand command = new OleDbCommand("CommandHere", ConnectionObject);
    OleDbDataReader reader; 
    int rows = 0;
     
    command.Connection.Open();
    reader = command.ExecuteReader();
    while (reader.Read())
    {
       rows +=1;
    }
    command.Connection.Close();
     
    MessageBox.Show("Results found: " + rows.ToString());

     

     

    is this what you are after? Does this help?

    Sunday, July 8, 2007 12:17 PM
  • Thank you ahmedilyas!

    The second example is great.

    Sunday, July 8, 2007 1:44 PM