c# - How to get row count from OdbcDataReader? -


I OdbcDataReader I'm used to getting results from my database and I know the line number Are the results ? Code: What is the best way

  public string to achieve this [] GetHeaderAndColumnValues ​​(string [] arrAllColumns, String [] arrExtColumns, string sRowDelimiterDisplayText, OdbcDataReader readerOdbc) {// Approach -1 will be to get line number, but this is an expensive approach (using Datatable DT = new datatable ()) {dt.Load (readerOdbc); Int i = dt.rows.Count; }}  

Note: I just need a line number from OdbcDataReader .

OK, you can not (and can not) find out how many lines are in the result set You read them all

and while OdbcDataReader is a RecordsAffectedProperty :. This is valid only for you, 'I have read all the rows, and

  • is inserted only for the inserted, deleted, or affected statements by the update statements.
  • It does not use too much to read. So, you can either

    • Each row count you read it:

        (OdbcConnection connection = new OdbcConnection (Connectionstring)) (OdbcCommand Command = connection.CreateCommand ()) {connection.Open (); order. Common Text = "Choose * from sys.objects"; (Using OdbcDataReader Reader = command.ExecuteReader ()) {int rowcount = 0; While (reader.Read ()) {++ rowcount; }}}  
    • Or, if you need to know the total rowcount to doing something with the results, your best choice is a dataAdapter Use something like this and accidentally results from huge mass :

        datatale dt = new datatyll (); Int rows; (OdbcConnection connection = new OdbcConnection (connectionstring) using using (OdbcCommand order = connection.CreateCommand ())) ({connection.Open (Using OdbcDataAdapter adapter = new OdbcDataAdapter (order))); order. Common Text = "Choose * from sys.objects"; Line = adapter Filled (DT); } Console.WriteLine ("adapter.Fill () {0}", rows returned); Console.light line ("The data table has {0} rows and {1} columns.", Dt.ows.Count, dt.Column.Count);  

      which is generated in my case,

        adapter.Fill () Back 74 data table has 74 rows and 12 columns. One third, a very useful option would be to run two questions: one that generates the line number:  
         code> select count Foo (*) count, where some terms  

      and the second, like the first, which lets the lines:

        Select foo from * where  

      ExecuteScalar () first and execute the second by using your data reader.

      There are two major issues in this approach:

      1. You are double the amount of work done.
      2. This presents a concurrent issue: Between calculations between first and second queries can change the line count well. / Li>

    Comments

    Popular posts from this blog

    android - TCP sending with Unity -

    graphviz - size of node with shape=circle -