Load data in datatable from SQL Server database
Loading data from database is very basic part of every application which uses database as data store. Let's see how we can load data in DataTable from database using ADO.NET from a SQL server database. DataTable dt = new DataTable (); using ( SqlConnection con = new SqlConnection ( ConfigurationManager .ConnectionStrings[ "MyConnectionString" ].ConnectionString)) { using ( SqlCommand cmd = new SqlCommand ()) { cmd.CommandText = "Select * from tblUsers" ; cmd.Connection = con; cmd.CommandType = CommandType .Text; using ( SqlDataAdapter adapter = new SqlDataAdapter (cmd)) { adapter.Fill(dt); } } }