ado dot net

4
ADO.NET: Ado.NET act like a bridge between front end and back end database. See the below diagram. Now Ado.NET has some class and objects. See below diagram. DataSet Class: Dataset class is called subset of database. But it is not actual database. When we want some data from database we use dataset class. But we do not directly connect database through dataset. We need some kind of adapter. When we have connection through dataset (actually through data adapter) and we want to add some data into table we need table. DataTable Class: The data table class represents the tables in the database. We use data row class to add row in the table.

Upload: salman-mushtaq

Post on 14-Feb-2017

198 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ADO DOT NET

ADO.NET:

Ado.NET act like a bridge between front end and back end database. See the below diagram.

Now Ado.NET has some class and objects. See below diagram.

DataSet Class:

Dataset class is called subset of database. But it is not actual database. When we want some data from

database we use dataset class.

But we do not directly connect database through dataset. We need some kind of adapter.

When we have connection through dataset (actually through data adapter) and we want to add some

data into table we need table.

DataTable Class:

The data table class represents the tables in the database. We use data row class to add row in the

table.

Page 2: ADO DOT NET

Finally we have some data and by using some properties we like add etc.. We enter data into table. But

remember this data is not permanently stored in database. Then how to add data permanently in

database. We talk later on this topic. This lesson is just for knowhow from ADO.NET.

See the below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>ADO.NET</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="GridView1"> </asp:GridView> </div> </form> </body> </html>

And the code behind this.

using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Make database, we know dataset is subset of database so we use dataset object DataSet ds = CreateDataSet(); GridView1.DataSource = ds.Tables["Students"]; GridView1.DataBind(); } } private DataSet CreateDataSet() { // Create object of dataset DataSet dataset = new DataSet();

Page 3: ADO DOT NET

//create table e.g. student, we know class datatable hace table so we use object // of datatable DataTable Students = CreateStudentTable(); //add this table to dataset dataset.Tables.Add(Students); return dataset; } private DataTable CreateStudentTable() { // first of all access Students table DataTable Students = new DataTable("Students"); // Add columns AddNewColumn(Students, "System.Int32", "StudentID"); AddNewColumn(Students, "System.String", "StudentName"); AddNewColumn(Students, "System.String", "StudentCity"); // Add rows AddNewRow(Students, 1, "Salman Mushtaq" , "Lahore"); AddNewRow(Students, 2, "Farzan Mehdi", "Lahore"); AddNewRow(Students, 3, "Umer Farooq", "Lahore"); return Students; } private void AddNewColumn(DataTable Students,string columnType, string columnName) { DataColumn column = Students.Columns.Add(columnName, Type.GetType(columnType) ); } private void AddNewRow(DataTable Students, int id, string name, string city) { DataRow row = Students.NewRow(); row["StudentID"] = id; row["StudentName"] = name; row["StudentCity"] = city; Students.Rows.Add(row); } }

So what this code said.

First see output of this code.

Page 4: ADO DOT NET

The application first creates a data set and binds it with the grid view control using the

DataBind() method of the GridView control.

The Createdataset() method is a user defined function, which creates a new DataSet

object and then calls another user defined method CreateStudentTable() to create the

table and add it to the Tables collection of the data set.

The CreateStudentTable() method calls the user defined methods AddNewColumn() and

AddNewRow() to create the columns and rows of the table as well as to add data to the

rows.

Hope you understand.

Refernce: www.tutorialpoint.com