Send Records Straight To DataTable

Discussion in 'ASP .Net' started by Beza, Jun 27, 2003.

  1. Beza

    Beza Guest

    I know how to use the Fill method of a SqlDataAdapter (into a DataSet) but
    it seems quite long winded, if I just want to get the results into a
    DataTable object. Is there a better way of doing this?
    Beza, Jun 27, 2003
    #1
    1. Advertising

  2. It isn't long winded at all.

    DataTable dt = new DataTable();
    SqlDataAdapter sa = new SqlDataAdapter();
    sa.SelectCommand = new SqlCommand();
    sa.SelectCommand.Connection = new SqlConnection( "connectionstring" );
    sa.SelectCommand.CommandText = "Select * from *";

    sa.SelectCommand.Connection.Open();
    sa.Fill( dt );

    You can combine some of these lines together with the constructor, but I was
    just showing you the easy way to follow.

    HTH,

    bill


    "Beza" <> wrote in message
    news:...
    > I know how to use the Fill method of a SqlDataAdapter (into a DataSet) but
    > it seems quite long winded, if I just want to get the results into a
    > DataTable object. Is there a better way of doing this?
    >
    >
    William F. Robertson, Jr., Jun 27, 2003
    #2
    1. Advertising

  3. Use a DataReader to get the data. Then create a new instance of a DataTable.
    Then loop through the DataReader, adding a row to the DataTable with each
    pass, and fill the columns of the row from the columns of the DataReader
    row.

    HTH,

    Kevin Spencer
    Microsoft FrontPage MVP
    Internet Developer
    http://www.takempis.com
    Big things are made up of
    lots of Little things.

    "Beza" <> wrote in message
    news:...
    > I know how to use the Fill method of a SqlDataAdapter (into a DataSet) but
    > it seems quite long winded, if I just want to get the results into a
    > DataTable object. Is there a better way of doing this?
    >
    >
    Kevin Spencer, Jun 27, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. J. Babe
    Replies:
    1
    Views:
    8,336
    Kevin Spencer
    Aug 15, 2003
  2. Fredrik Rodin

    Sort a datatable and create a new datatable

    Fredrik Rodin, Feb 16, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    9,041
    paul.mariotti
    Sep 28, 2007
  3. =?Utf-8?B?Qmx1ZWZsb3dlcg==?=

    Casting DataTable to class inherit from DataTable

    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=, Apr 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    992
    =?Utf-8?B?Qmx1ZWZsb3dlcg==?=
    Apr 2, 2004
  4. jg
    Replies:
    1
    Views:
    720
    =?Utf-8?B?U2hhaWxlc2ggSmFubnU=?=
    Aug 17, 2004
  5. Ersin Gençtürk
    Replies:
    1
    Views:
    3,477
    Ersin Gençtürk
    Oct 6, 2004
Loading...

Share This Page