Reading data into a dataset in C#


/ Published in: C#
Save to your folder(s)

Snip it if you keep forgetting how its done.


Copy this code and paste it in your HTML
  1. using System.Data;
  2. using System.Data.SqlClient;
  3. using System.Configuration;
  4.  
  5. var connectionStr = ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString;
  6. SqlCommand cmd = new SqlCommand();
  7. cmd.Connection = new SqlConnection(connectionStr);
  8. cmd.Connection.Open();
  9. cmd.CommandText = "SELECT name, lastName FROM myTable";
  10. var users = new DataSet();
  11. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  12. adapter.Fill(users);
  13. cmd.Connection.Close();
  14. cmd.Connection.Dispose();

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.