VND - Ms Powerpoint&Rendition 1
VND - Ms Powerpoint&Rendition 1
VND - Ms Powerpoint&Rendition 1
Net Training
Program
ajp.trichy@gmail.com
ADO.NET
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
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;
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;
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;
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