Search This Blog

Wednesday, March 20, 2013

Populating a DataSet and table from a DataAdapter C#

C# > Data >  Populating DataSet and DataTable

SQL Adapter to executes the SQL statement or stored procedure referenced in its SelectCommand and put the results into a table in the dataset.
Than you call data adapter's Fill method.  
If the dataset contains multiple tables, you should have separate data adapters for each table and must therefore fill each table separately.

Example:

SqlConnection con = new SqlConnection("connString");
SqlCommand com = new SqlCommand("exec your_stored_procedure", con);
com.CommandTimeout = 30;
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = com;
con.Open();
DataSet ds = new DataSet();
da.Fill(ds, "Data");
DataTable dt = ds.Tables[0];
con.Close();