Load data from SqlDataReader into DataTable

SqlDatareader is used in .Net to read data from sql server database. We may have a situation where we need to fill data into DataTable from SqlDataReader. DatatTable has a Loadmethod which accepts SqlDataReader object and fills DataTable with data.

DataTabledt = new DataTable();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
{
   using (SqlCommand cmd = new SqlCommand())
   {
      con.Open();
      cmd.CommandText = "Select * from tblUsers";
      cmd.Connection = con;
      cmd.CommandType = CommandType.Text;
      SqlDataReader reader = null;
      reader = cmd.ExecuteReader();
      dt.Load(reader);
                   
    }

}

Comments

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Using Progress Bar In C#

Jquery serer side datatables in asp.net