Posted By

fengelz on 04/04/11


Tagged

database sql c#


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Priestd09
danfinrud55


Reading data into a dataset in C#


 / Published in: C#
 

Snip it if you keep forgetting how its done.

  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  

You need to login to post a comment.