How to access database using C#. Summary of ways and tutorial on how to retrieve data from a database using c#.NET.

DATABASE ACCESS C#

C# Tutorials

  

DATABASE ACCESS C SHARP

Learn Tutorials
How to access database using C#. Summary of ways and tutorial on how to retrieve data from a database using c#.NET.

Example code for Database access c sharp:
Datareaders, databinding, prepopulating forms


Basic use of DataReader (often for binding to a control):

   SqlConnection conn = new SqlConnection(connectionString);
   SqlCommand command = new SqlCommand("SELECT * FROM employees",conn);
   conn.Open();
   
   SqlDataReader reader = commmand.ExecuteReader();//dumps the result set into the data reader
   
   //do something with the reader such as bind to a control:
      repeaterControl.DataSource=reader; *
      repeaterControl.DataBind();
   
   //or dump results
      while(reader.Read())
         Response.Write(reader["lname"]+"<br>");
   
   //or prepopulate a form
      while(reader.Read()){
         txtFname.Text = (string) reader["fname"];
         txtLname.Text = (string) reader["lname"];
      }
   
   
   reader.Close();
   conn.Close();
   
   
* datasource can be any object that implement IEnumerable (or IList?), such as
DataReader, DataView, DataColumn, DataTable, DataSet, ArrayList, Hashtable, array etc.
   
You can bind data to numerous controls including Lists, DropDownLists, Menus, CheckBoxLists, RadioButtonLists, TextBoxes, DataGrids, DataLists, Repeaters etc.   
      
No reader is used if you are doing an insert, update,delete since nothing returned. would instead do:
   
   //these first 3 are same as above
   SqlConnection conn = new SqlConnection(connectionString);
   SqlCommand command = new SqlCommand("Delete * FROM employees");
   conn.Open();
   
   command.ExecuteNonQuery();
   
   conn.Close();
   
         
To use parameters in a command:
   
SqlCommand commmand = new SqlCommand("SELECT * FROM employees where lname=@lname",conn);
command.Parameters.Add("@lname", txtSearchbox.Text);

OR if a listbox/dropdownlist:
   command.Parameters.Add("@EmployeeID",lbEmployees.SelectedItem.Value);




Binding:

all start with:

   SqlConnection conn = new SqlConnection(connectionString);
   SqlCommand commmand = new SqlCommand("SELECT * FROM products",conn);
   conn.Open();

Binding DropDown Lists (select boxes) and ListBoxes:

   SqlDataReader reader = commmand.ExecuteReader();
   ddl.DataSource= reader;
   ddl.DataValueField = "prodID";
   ddl.DataTextField = "ProductName";
   ddl.DataBind();
   reader.Close();
   
Binding Repeaters:

   SqlDataReader reader = commmand.ExecuteReader();
   repeaterControl.DataSource=reader;
   repeaterControl.DataBind();
   reader.Close();
   
all end with:
   conn.Close();
   
   
To bind to multiple controls must do the ExecuteReader() and Close() for each.
This is example code for how to access database using c#. summary of ways and tutorial on how to retrieve data from a database using c#.net.