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);  
      }
    }

 }


Comments

Popular posts from this blog

Using Progress Bar In C#

Check if ViewBag is null or doesn't exist

Jquery serer side datatables in asp.net