Send Records Straight To DataTable

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?
 
W

William F. Robertson, Jr.

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
 
K

Kevin Spencer

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top