ADO.
Net Training
Program
J. A. John Prabu, MCA, MCTS, M.Phil,
Assistant Professor,
Dept. of Computer Science,
St. Joseph’s College (Autonomous),
Tiruchirappalli, Tamilnadu
[email protected]
ADO.NET
ADO.NET takes its name from ADO(ActiveX Data Objects)
ADO.NET classes are located in the System.Data.dll
assembly.
Features of ADO.NET
Disconnected data architecture
Data stored in dataset
Data transfer in XML format
ADO.NET Classes and Objects
ADO.NET classes can be divided into provider objects
and consumer objects.
1. Provider objects -- data source
Reading and writing to and from the
data sources
2. Consumer objects -- used to access and manipulate the
data in disconnected fashion.
ADO.NET Provider Objects
Connection
Command
CommandBuilder
DataReader
DataAdapter
ADO.NET Provider Objects
Connection Object:
It provides basic connection to our data source. It is the
first one.
Command Object:
This object is used to give a command such as SQL query
to a data source.
CommandBuilder Object:
It is used to build SQL commands for data modifications.
DataReader Object :
This is fast, simple to user object that reads a
forward-only and read-only stream of data from a data
source.
DataAdapter Object:
It is used for updating changed data,
filling data sets and other operations.
ADO.NET Consumer Objects
Data Set
DataTable
DataRow
DataColumn
DataRelation
ADO.NET Consumer Objects
DataSet Object :
This object represents a set of related tables referenced
as one unit in our application.
DataTable Object:
This object represents table in DataSet.
DataRow Object:
This Object represents one row of related data from
table.
DataColumn Object:
This Object represents one column in the table.
DataRelation Object:
This object represents the relationship between two
tables via a shared column.
ADO.NET Object Model
Data Accessing from Sql Server
Add the following namespace
using System.Data;
using System.Data.SqlClient;
Data Accessing from Sql Server
Connection:
SqlConnection conn;
conn = new SqlConnection("Data
Source=JOHNPRABU;InitialCatalog=sjc;Integrated
Security=True");
conn.Open();
Insert:
SqlCommand cmd;
cmd = new SqlCommand("Insert Into EmpDetails
values( '" + empid.Text+"','" + name.Text + "','" +
address.Text + "','" + city.Text + "','" + pin.Text + "','" +
cell.Text + "')",conn);
cmd.ExecuteNonQuery();
Data Accessing from Sql Server
Delete:
SqlCommand cmd;
cmd = new SqlCommand("Delete EmpDetails where
EmpID='" + empid.Text + "'",conn);
cmd.ExecuteNonQuery();
Update:
SqlCommand cmd;
cmd = new SqlCommand("Update EmpDetails SET
EmpID='" + empid.Text + "',EName='" + name.Text +
"',Address='" + address.Text + "',City='" + city.Text +
"',Pin='" + pin.Text + "',Cell='" + cell.Text + "' where
EmpID='" + empid.Text + "' ", conn);
cmd.ExecuteNonQuery();
Data Accessing from Sql Server
Search:
protected void Search_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlDataReader dr;
cmd = new SqlCommand("Select * from EmpDetails where EmpID='" + empid.Text
+ "‘ ",conn);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
while (dr.Read())
{
name.Text = dr["EName"].ToString();
address.Text = dr["Address"].ToString();
city.Text = dr["City"].ToString();
pin.Text = dr["Pin"].ToString();
cell.Text = dr["Cell"].ToString();
}
dr.Close();
}
Accessing a field and show it in the
DropdownListBox:
protected void searchfn()
{
SqlCommand cmd;
SqlDataReader dr;
cmd = new SqlCommand("Select * from EmpDetails", conn);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "EName";
this.DropDownList1.DataValueField = "EName";
this.DropDownList1.DataBind();
dr.Close();
dr = cmd.ExecuteReader();
this.DropDownList2.DataSource = dr;
this.DropDownList2.DataTextField = "City";
this.DropDownList2.DataValueField = "City";
this.DropDownList2.DataBind();
dr.Close();
cmd.Dispose();
}
Search and view result in the Gridview:
Use DataReader:
protected void datareader_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlDataReader dr;
cmd = new SqlCommand("Select * from EmpDetails",con);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
GridView1.DataSource = dr;
GridView1.DataBind();
cmd.Dispose();
dr.Close();
}
Data Accessing from Sql Server
Using DataTable:
protected void datatable_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlDataReader dr;
DataTable Mytable;
cmd = new SqlCommand("Select * from EmpDetails", con);
cmd.ExecuteNonQuery();
dr = cmd.ExecuteReader();
Mytable = new DataTable();
Mytable.Load(dr);
GridView2.DataSource = Mytable;
GridView2.DataBind();
dr.Close(); Mytable.Dispose();
cmd.Dispose();
}
Data Accessing from Sql Server
Using Dataset:
protected void dataset_Click(object sender, EventArgs e)
{
SqlCommand cmd;
SqlDataAdapter da;
Dataset ds;
cmd = new SqlCommand("Select * from EmpDetails", con);
cmd.ExecuteNonQuery();
cmd1 = new SqlCommand("Select * from studentdls", con);
cmd1.ExecuteNonQuery();
da =new SqlDataAdapter();
da.SelectCommand = cmd;
ds = new DataSet();
da.Fill(ds, "EmpDetails");
da.SelectCommand = cmd1;
da.Fill(ds, "studentdls");
GridView3.DataSource = ds.Tables["EmpDetails"].DefaultView;
GridView3.DataBind();
GridView4.DataSource = ds.Tables["studentdls"].DefaultView;
GridView4.DataBind();
ds.Dispose();
da.Dispose();
cmd.Dispose();
}
Thank You