Steps to fill data in to a Dataset object - ablealias/asp.net GitHub Wiki

  • First define a String variable to store database Connection String and get the connection string which configured on WebConfig file.
private string connectionString;
this.connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["sqlDatabase"].ConnectionString;

  • Create a SqlConnection object which is used to connect to the Database, provide the connection string as parameter.
SqlConnection connection = new SqlConnection(connetionString); 
connection.Open();
  • Create an SqlDataAdapter object to execute your SQL statements either SELECT query or a Stored Procedure
SqlDataAdapter adapter = new SqlDataAdapter("Your SQL Statement Here", connection);
  • Create a DataSet object to fill the SQL Data which is stored on the SqlDataAdapter
DataSet ds = new DataSet(); 
adapter.Fill(ds); 
connection.Close(); 
  • if you want to go through the dataset data, just put foreach loop through the dataset table rows,
foreach (DataRow dr in ds.Tables[0].Rows)
{
    
}

Example with Stored Procedure

SqlDataAdapter adapter = new SqlDataAdapter("Procedure_Name",connection);
            adapter.SelectCommand.CommandType = CommandType.StoredProcedure;
            adapter.SelectCommand.Parameters.Add("@Contactid", SqlDbType.Int).Value = 123;
            DataSet ds = new DataSet();
            adapter.Fill(ds);