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 SHARPLearn 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. |
Home
CSHARP Arrays in c sharp C sharp array objects C sharp separators context menu Compare dates csharp datetime Context menu c sharp Context menus csharp example Controls c sharp dynamically Database access c sharp Database access with ADO NET DataGrid c sharp Datagrid dataset csharp NET Datagrid Styles data grid Datasets listview dropdown selectbox Dialogbox csharp dialog box Drag and drop csharp Listview combo box csharp Looping through hashtable NET NET collections tutorial NET crawler safe url One row from dataset Output datetime csharp Popup form csharp Try catch c sharp |
||||||
![]() | |||||||