Retrieving rows during execution. RRS feed

  • Question

  • Hi everybody,

    I need to integrate a kind of "query analyzer" inside my forms application.
    The user has the possibility to type his own query and execute it with button.

    My question is:
    How can I execute the query (with ExecuteReader or with the fill method of the DataAdapter) and in same time, already display the retrieved rows ? Like QueryAnalyzer tool.

    Maybe the query will return 50.000 rows, but I don't want to wait for the end of execution to see the result.
    I would like to begin the display (of rows) since the query is still in execution.

    The goal is not to modify the query, so I cannot change the query and add the "top" clause to return only 'x' first records.

    I found a solution in executing the query with the DataAdapter.
    => _daAdapter.Fill(_dtResult);
    In another process, I read the dataTable while the first process continue to execute it (to fill it).
    I read the data from the data table and I load a datagridview.

    I would like to know if it's the right solution ?
    Or if you have another one ?

    Thanks in advance
    Tuesday, July 10, 2007 2:53 PM

All replies

  • You shouldn't be loading a DataGridView from a DataTable, you should be binding it.  That'll save you a whole lot of code.  The BindingSource detects when a row has been added to its data source (the DataTable) and pushes the data to all of its bound controls automatically.

    The next trick is to run the Fill method in a different thread than the one running the UI.  To do that, you want to create a BackgroundWorker control on your form and put the Fill method in its DoWork event handler.  Then you call RunWorkerAsync to start the query running.

    DataTable is thread-safe for read operations, so as long as you don't let your users modify rows in the DataGridView until the query's done running you won't run into threading problems.  If you want to let your users edit rows before the query completes, you'll need to deal with synchronizing the write operation, and frankly I don't know how you'd do that.

    Thursday, July 12, 2007 4:17 PM
  • Hi,

    Thanks for your reply.

    I load the DataGridView like this:
    dataGridView1.DataSource = MyDataTable;

    For the BackGroundWorker control, I need to read the documentation, because I never used it.
    But I have already a asynchronous process to allow the cancel command.


    Monday, July 16, 2007 8:43 AM