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
Post a Comment