Send Records Straight To DataTable

  • Thread starter Thread starter Beza
  • Start date Start date
B

Beza

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?
 
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
 
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.
 
Back
Top