Posts

Showing posts with the label SqlDataReader

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 Load method which accepts SqlDataReader object and fills DataTable with data. DataTable dt = 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);                ...