Hi friends
C# Tutorial
Part 37:ADO.net
www.siri-kt.blogspot.com
• ADO is nothing but active x data objects. we can communicate
between front end to backend using ado.net
• Front end is nothing but c#.net, vb.net, Asp.net etc
• Back end is nothing but database sql server, oracle, ms-access, ms-
excel
• ADO.net supports different providers
• 1.using system.data.sqlclient;
• 2.using system.data.oracleclient;
• 3.using system.data.odbc;(ms-access,ms-excel);
• 4.using system.data.oledb;(sqlserver & oracle);
• odbc-open database connectivity. oledb-object linked embedded
database
• ado.net supports 2 types of connection models:
• 1.connection oriented model
• 1.connection oriented model: in this model front end to Back end
directly relation is possible. connection flow:
• connection-con.open()-command-UI
• 1.connection: this class establishes the communication bw front end
to backend.
• this class supports 2 methods
• 1.open():using this method we can open the established connection
way.
• 2.close(): using this method we can close the opened connection way.
• command class:
• this class is used to send sql statement request from the f.e
• and for getting the response.
• this class supports 2 parameters
• 1.sql statements
• This class supports 3 methods
• 1.executenonquery()-insert,update,delete statements
• 2.executescalar()-authentication logic
• 3.executeReader()-Readonly data,forward the records.this
method
• Returns datareader class.
• Data reader class supports 2 methods
• 1.Read()
• 2.close()
• Example on connection oriented model:
• 1.take the form
• 2.add 3 labels,3 textboxes and 3 buttons
• using System.Data.SqlClient;
• private void button1_Click(object sender, EventArgs e)
• { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• con.Open();
• SqlCommand cmd = new SqlCommand("insert into student values(" + textBox1.Text + ",'" +
textBox2.Text + "','" + textBox3.Text + "')", con);
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are inserted"); }
• private void button2_Click(object sender, EventArgs e)
• { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• con.Open();
• SqlCommand cmd = new SqlCommand("update student set sname='" + textBox2.Text + "',saddress='" +
textBox3.Text + "' where sno=" + textBox1.Text, con);
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are updated"); }
• private void button3_Click(object sender, EventArgs e)
• { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• con.Open();
• SqlCommand cmd = new SqlCommand("delete from student where sno=" + textBox1.Text, con);
• Example on connection oriented in oledb:
• 1.take the form
• 2.add 3 labels,3 textboxes and 3 buttons
• source code:
• using System.Data.OleDb;
• private void button1_Click(object sender, EventArgs e)
• { OleDbConnection con = new
OleDbConnection("provider=sqloledb.1;data source=sit-
pc;database=suhashini;user id=sa;password=123");
• con.Open();
• OleDbCommand cmd = new OleDbCommand("insert into student
values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text +
"')", con);
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are inserted");
• private void button2_Click(object sender, EventArgs e)
• { OleDbConnection con = new
OleDbConnection("provider=sqloledb.1;data source=sit-
pc;database=suhashini;user id=sa;password=123");
• con.Open();
• OleDbCommand cmd = new OleDbCommand("update student set
sname='" + textBox2.Text + "',saddress='" + textBox3.Text + "' where
sno=" + textBox1.Text, con);
• cmd.ExecuteNonQuery();
• MessageBox.Show("values are updated"); }
• private void button3_Click(object sender, EventArgs e)
• { OleDbConnection con = new
OleDbConnection("provider=sqloledb.1;data source=sit-
pc;database=suhashini;user id=sa;password=123");
• con.Open();
• OleDbCommand cmd = new OleDbCommand("delete from student
where sno=" + textBox1.Text, con);
• cmd.ExecuteNonQuery();
• disconnection model:
• In the disconnection model front end to back end
directly relation is not possible . we can bridge the
communication between front end to back end using data
adapter.
• disconnection flow:
• connection-data adapter-dataset-da.fill(ds,dt)-ui
• data bounded controls:
• using this controls back end data base values bind with
the front end
• displayed in tabular format.
• c#.net supports no of data bounded controls
• 1.datagridview
• 2.dataset
• 3.datagrid
• example on disconnection flow:
• 1.take the form
• 2.add datagridview control
• 3.add button control
• using System.Data.SqlClient;
• private void button1_Click(object sender, EventArgs e)
• { SqlConnection con = new SqlConnection("data source=sit-
pc;database=suhashini;user id=sa;password=123");
• SqlDataAdapter da = new SqlDataAdapter("select * from student", con);
• DataSet ds = new DataSet();
• da.Fill(ds);
• dataGridView1.DataSource = ds.Tables[0]; }
• example on datareader class:
• 1.take the form
• 2.add 3 labels,3 textboxes,3 buttons
• source code:
• SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• SqlCommand cmd;
• SqlDataReader dr;
• private void button1_Click(object sender, EventArgs e)
• { con.Open();
• cmd = new SqlCommand("select * from student", con);
• dr = cmd.ExecuteReader();
• dr.Read();
• textBox1.Text = dr[0].ToString();
• textBox2.Text = dr[1].ToString();
• textBox3.Text = dr[2].ToString(); }
• private void button2_Click(object sender, EventArgs e)
• { if (dr.Read())
• { textBox1.Text = dr[0].ToString();
• textBox2.Text = dr[1].ToString();
• textBox3.Text = dr[2].ToString();
• } else
• { MessageBox.Show("EOR");
• } }
• Dataadapter:
• it is bridge the communication between front end to back end
• this class having 2 parameters
• 1.select statement
• 2.connection object
• methods:
• 1.fill(ds,dt):retrieve the data from dataset
• syntax:
• da.fill(ds,dt)
• ds=dataset
• dt=datatable
• 2.update(ds,dt)
• syntax:
• da.updat(ds,dt)
• ds=dataset
• dt=datatable
• Datagridview:
• this control supports a only single table at a time
• Datagrid:
• this control supports multiple tables at a time. this control supports primary key and
foriegn key tables
• example on datagrid control:
• 1.take the form
• 2.add datagrid control
• source code:
• using System.Data.SqlClient;
• private void button1_Click(object sender, EventArgs e)
• { SqlConnection con = new SqlConnection("data source=sit-
pc;database=suhashini;user id=sa;password=123");
• SqlDataAdapter da1 = new SqlDataAdapter("select * from dept", con);
• SqlDataAdapter da2 = new SqlDataAdapter("select * from emp", con);
• DataSet ds = new DataSet(); da1.Fill(ds, "d"); da2.Fill(ds,"e");
dataGrid1.DataSource = ds;
• commandbuilder :
• This class is used to generate sql statements this class takes columns information from
data adapter
• This class supports 3 methods
• 1.GetInsertCommand()
• 2.GetUpdatecommand()
• 3.GetDeletecommand()
• 1.GetInsertcommand(): this method generates insert sql statements
• syntax: da.insertcommand=cmb.Getinsertcommand();
• 2.GetUpadatecommand(): this method generates update sql statements
• syntax: da.updatecommand=cmb.getupdatecommand();
• 3.GetDeletecommand():this method generates delete sql statements
• da.deletecommand=cmb.getdeletecommand();
• Example on Commandbuilder:
• 1.take the 1 form
• 2.add datagrid control
• 3.add 3 labels ,3 textboxes and 3 buttons
• Source Code:
• SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• SqlDataAdapter da;
• DataSet ds = new DataSet();
• DataTable dt = new DataTable();
• DataRow dr;
• SqlCommandBuilder cmb;
• private void button1_Click(object sender, EventArgs e)
• { da = new SqlDataAdapter("select * from student", con);
• da.Fill(ds, "s"); dt = ds.Tables["s"]; dataGrid1.DataSource = dt;
• }
• private void button2_Click(object sender, EventArgs e)
• { da = new SqlDataAdapter("select * from student",
con);
• cmb = new SqlCommandBuilder(da);
• dr = dt.NewRow();
• dr["sno"] = Convert.ToInt16(textBox1.Text);
• dr["sname"] = textBox2.Text; dr["saddress"] =
textBox3.Text;
• dt.Rows.Add(dr); da.InsertCommand =
cmb.GetInsertCommand();
• da.Update(ds, "s");
• }
• private void button3_Click(object sender, EventArgs e)
• { da = new SqlDataAdapter("select * from student",
con);
• cmb = new SqlCommandBuilder(da);
• da.UpdateCommand = cmb.GetUpdateCommand();
• Dataset using data relation:
• While creating relations in the dataset at least we have
to work with 2 –tables while creating the tables we
have to create tables with primary key constraint & with
foreign key constraint
• which table having primary key constraint column that
table is master table. which table having foreign key
constraint column that that table is child table.
• master table:
• dept
• dno int(primary key)
• dname varchar(50)
• daddress varchar(50)
• Emp
• eno int
• ename varchar(50)
• dno int (foreign key)
• Data Relation:
• this class is used to generate relations in the dataset. this class
takes 3 constructor parameters
• 1.Relation name
• 2. Primary key constraint column
• 3. foreign key constraint column
• example on data relation:
• 1.take the form
• 2.Add datagrid control
• 3.add 1 button control
• source code:
• SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user
id=sa;password=123");
• SqlDataAdapter da1, da2;
• DataSet ds = new DataSet();
• DataRelation dr;
• private void button1_Click(object sender, EventArgs e)
• { da1 = new SqlDataAdapter("select * from dept", con);
• da2 = new SqlDataAdapter("select * from emp", con);
• da1.Fill(ds, "d"); da2.Fill(ds, "e");
• DataColumn pk = ds.Tables["d"].Columns["dno"]; DataColumn fk =
ds.Tables["e"].Columns["dno"];
• dr = new DataRelation("rel", pk, fk); ds.Relations.Add(dr);
dataGrid1.DataSource = ds;
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching

37c

  • 1.
    Hi friends C# Tutorial Part37:ADO.net www.siri-kt.blogspot.com
  • 2.
    • ADO isnothing but active x data objects. we can communicate between front end to backend using ado.net • Front end is nothing but c#.net, vb.net, Asp.net etc • Back end is nothing but database sql server, oracle, ms-access, ms- excel • ADO.net supports different providers • 1.using system.data.sqlclient; • 2.using system.data.oracleclient; • 3.using system.data.odbc;(ms-access,ms-excel); • 4.using system.data.oledb;(sqlserver & oracle); • odbc-open database connectivity. oledb-object linked embedded database • ado.net supports 2 types of connection models: • 1.connection oriented model
  • 3.
    • 1.connection orientedmodel: in this model front end to Back end directly relation is possible. connection flow: • connection-con.open()-command-UI • 1.connection: this class establishes the communication bw front end to backend. • this class supports 2 methods • 1.open():using this method we can open the established connection way. • 2.close(): using this method we can close the opened connection way. • command class: • this class is used to send sql statement request from the f.e • and for getting the response. • this class supports 2 parameters • 1.sql statements
  • 4.
    • This classsupports 3 methods • 1.executenonquery()-insert,update,delete statements • 2.executescalar()-authentication logic • 3.executeReader()-Readonly data,forward the records.this method • Returns datareader class. • Data reader class supports 2 methods • 1.Read() • 2.close() • Example on connection oriented model: • 1.take the form • 2.add 3 labels,3 textboxes and 3 buttons
  • 5.
    • using System.Data.SqlClient; •private void button1_Click(object sender, EventArgs e) • { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • con.Open(); • SqlCommand cmd = new SqlCommand("insert into student values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", con); • cmd.ExecuteNonQuery(); • MessageBox.Show("values are inserted"); } • private void button2_Click(object sender, EventArgs e) • { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • con.Open(); • SqlCommand cmd = new SqlCommand("update student set sname='" + textBox2.Text + "',saddress='" + textBox3.Text + "' where sno=" + textBox1.Text, con); • cmd.ExecuteNonQuery(); • MessageBox.Show("values are updated"); } • private void button3_Click(object sender, EventArgs e) • { SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • con.Open(); • SqlCommand cmd = new SqlCommand("delete from student where sno=" + textBox1.Text, con);
  • 6.
    • Example onconnection oriented in oledb: • 1.take the form • 2.add 3 labels,3 textboxes and 3 buttons • source code: • using System.Data.OleDb; • private void button1_Click(object sender, EventArgs e) • { OleDbConnection con = new OleDbConnection("provider=sqloledb.1;data source=sit- pc;database=suhashini;user id=sa;password=123"); • con.Open(); • OleDbCommand cmd = new OleDbCommand("insert into student values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", con); • cmd.ExecuteNonQuery(); • MessageBox.Show("values are inserted");
  • 7.
    • private voidbutton2_Click(object sender, EventArgs e) • { OleDbConnection con = new OleDbConnection("provider=sqloledb.1;data source=sit- pc;database=suhashini;user id=sa;password=123"); • con.Open(); • OleDbCommand cmd = new OleDbCommand("update student set sname='" + textBox2.Text + "',saddress='" + textBox3.Text + "' where sno=" + textBox1.Text, con); • cmd.ExecuteNonQuery(); • MessageBox.Show("values are updated"); } • private void button3_Click(object sender, EventArgs e) • { OleDbConnection con = new OleDbConnection("provider=sqloledb.1;data source=sit- pc;database=suhashini;user id=sa;password=123"); • con.Open(); • OleDbCommand cmd = new OleDbCommand("delete from student where sno=" + textBox1.Text, con); • cmd.ExecuteNonQuery();
  • 8.
    • disconnection model: •In the disconnection model front end to back end directly relation is not possible . we can bridge the communication between front end to back end using data adapter. • disconnection flow: • connection-data adapter-dataset-da.fill(ds,dt)-ui • data bounded controls: • using this controls back end data base values bind with the front end • displayed in tabular format.
  • 9.
    • c#.net supportsno of data bounded controls • 1.datagridview • 2.dataset • 3.datagrid • example on disconnection flow: • 1.take the form • 2.add datagridview control • 3.add button control • using System.Data.SqlClient; • private void button1_Click(object sender, EventArgs e) • { SqlConnection con = new SqlConnection("data source=sit- pc;database=suhashini;user id=sa;password=123"); • SqlDataAdapter da = new SqlDataAdapter("select * from student", con); • DataSet ds = new DataSet(); • da.Fill(ds); • dataGridView1.DataSource = ds.Tables[0]; }
  • 10.
    • example ondatareader class: • 1.take the form • 2.add 3 labels,3 textboxes,3 buttons • source code: • SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • SqlCommand cmd; • SqlDataReader dr; • private void button1_Click(object sender, EventArgs e) • { con.Open(); • cmd = new SqlCommand("select * from student", con); • dr = cmd.ExecuteReader(); • dr.Read(); • textBox1.Text = dr[0].ToString(); • textBox2.Text = dr[1].ToString(); • textBox3.Text = dr[2].ToString(); }
  • 11.
    • private voidbutton2_Click(object sender, EventArgs e) • { if (dr.Read()) • { textBox1.Text = dr[0].ToString(); • textBox2.Text = dr[1].ToString(); • textBox3.Text = dr[2].ToString(); • } else • { MessageBox.Show("EOR"); • } } • Dataadapter: • it is bridge the communication between front end to back end • this class having 2 parameters • 1.select statement • 2.connection object
  • 12.
    • methods: • 1.fill(ds,dt):retrievethe data from dataset • syntax: • da.fill(ds,dt) • ds=dataset • dt=datatable • 2.update(ds,dt) • syntax: • da.updat(ds,dt) • ds=dataset • dt=datatable
  • 13.
    • Datagridview: • thiscontrol supports a only single table at a time • Datagrid: • this control supports multiple tables at a time. this control supports primary key and foriegn key tables • example on datagrid control: • 1.take the form • 2.add datagrid control • source code: • using System.Data.SqlClient; • private void button1_Click(object sender, EventArgs e) • { SqlConnection con = new SqlConnection("data source=sit- pc;database=suhashini;user id=sa;password=123"); • SqlDataAdapter da1 = new SqlDataAdapter("select * from dept", con); • SqlDataAdapter da2 = new SqlDataAdapter("select * from emp", con); • DataSet ds = new DataSet(); da1.Fill(ds, "d"); da2.Fill(ds,"e"); dataGrid1.DataSource = ds;
  • 14.
    • commandbuilder : •This class is used to generate sql statements this class takes columns information from data adapter • This class supports 3 methods • 1.GetInsertCommand() • 2.GetUpdatecommand() • 3.GetDeletecommand() • 1.GetInsertcommand(): this method generates insert sql statements • syntax: da.insertcommand=cmb.Getinsertcommand(); • 2.GetUpadatecommand(): this method generates update sql statements • syntax: da.updatecommand=cmb.getupdatecommand(); • 3.GetDeletecommand():this method generates delete sql statements • da.deletecommand=cmb.getdeletecommand();
  • 15.
    • Example onCommandbuilder: • 1.take the 1 form • 2.add datagrid control • 3.add 3 labels ,3 textboxes and 3 buttons • Source Code: • SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • SqlDataAdapter da; • DataSet ds = new DataSet(); • DataTable dt = new DataTable(); • DataRow dr; • SqlCommandBuilder cmb; • private void button1_Click(object sender, EventArgs e) • { da = new SqlDataAdapter("select * from student", con); • da.Fill(ds, "s"); dt = ds.Tables["s"]; dataGrid1.DataSource = dt; • }
  • 16.
    • private voidbutton2_Click(object sender, EventArgs e) • { da = new SqlDataAdapter("select * from student", con); • cmb = new SqlCommandBuilder(da); • dr = dt.NewRow(); • dr["sno"] = Convert.ToInt16(textBox1.Text); • dr["sname"] = textBox2.Text; dr["saddress"] = textBox3.Text; • dt.Rows.Add(dr); da.InsertCommand = cmb.GetInsertCommand(); • da.Update(ds, "s"); • } • private void button3_Click(object sender, EventArgs e) • { da = new SqlDataAdapter("select * from student", con); • cmb = new SqlCommandBuilder(da); • da.UpdateCommand = cmb.GetUpdateCommand();
  • 17.
    • Dataset usingdata relation: • While creating relations in the dataset at least we have to work with 2 –tables while creating the tables we have to create tables with primary key constraint & with foreign key constraint • which table having primary key constraint column that table is master table. which table having foreign key constraint column that that table is child table. • master table: • dept • dno int(primary key) • dname varchar(50) • daddress varchar(50)
  • 18.
    • Emp • enoint • ename varchar(50) • dno int (foreign key) • Data Relation: • this class is used to generate relations in the dataset. this class takes 3 constructor parameters • 1.Relation name • 2. Primary key constraint column • 3. foreign key constraint column
  • 19.
    • example ondata relation: • 1.take the form • 2.Add datagrid control • 3.add 1 button control • source code: • SqlConnection con = new SqlConnection("data source=sit-pc;database=suhashini;user id=sa;password=123"); • SqlDataAdapter da1, da2; • DataSet ds = new DataSet(); • DataRelation dr; • private void button1_Click(object sender, EventArgs e) • { da1 = new SqlDataAdapter("select * from dept", con); • da2 = new SqlDataAdapter("select * from emp", con); • da1.Fill(ds, "d"); da2.Fill(ds, "e"); • DataColumn pk = ds.Tables["d"].Columns["dno"]; DataColumn fk = ds.Tables["e"].Columns["dno"]; • dr = new DataRelation("rel", pk, fk); ds.Relations.Add(dr); dataGrid1.DataSource = ds;
  • 20.
    For more visitour website www.siri-kt.blogspot.com Thanks for Watching