You are on page 1of 9

ADO.

NET

Frontend is nothing but an application through which data of a database will be represented
in a customized manner whereas backend is nothing but a database where data is stored as well as
processed.

.NET languages are not only capable of developing standalone applications but also capable
of developing front end applications which have their own backend.

To connect the frontend with the backend a technology is provided by .NET called ADO.NET
(Active-X Data Object.NET).

ADO.NET is not at all a new technology rather than extended version of ADO. The basic
difference between ADO and ADO.NET is, ADO supports connected architecture whereas ADO.NET
supports disconnected architecture.

Connected architecture means whenever the form is connected with the required
table(database), the connection is established and remains live. When any record manipulation or
record navigation is done, the request will be made to the server which leads to the following
disadvantages:
1. Makes network traffic busy
2. Leads to data loss
3. Slower processing

By eliminating the loopholes (disadvantages) of ADO, a new technology is introduced called


ADO.NET. In this technology when the table is connected, it will navigate to client virtually and the
connection with database is detached.

Now data searching and record navigation is done in client side whereas record
manipulation is done in server side which leads to following advantages:
1. Faster processing
2. Makes network traffic free
3. Less data loss

To implement ADO.NET, .NET provides 4 primary objects i.e.


a. Connection object
b. Adaptor object
c. Dataset object
d. Command object

Connection object is the primary object in ADO.NET architecture responsible to establish


the connection with the data. It requires the following properties:
i. Database type
ii. Database name
iii. User name
iv. Password

Once the connection is established the required table will be kept inside the adapter object.
In other words, adapter object contains the actual table information.

Once the adapter gets the table, will send it to client in form of dataset and the connection
is detached. Now any record navigation or record searching will be done using dataset.

Command object is not basically a part of ADO.NET rather than part of adapter object
through which record manipulation and searching are done.
Graphical representation of the ADO.NET architecture:
https://www.guru99.com/c-sharp-access-database.html
INSERTING RECORD USING ASP.NET C# AND ORACLE 11g

protected void Button1_Click(object sender, EventArgs e)


{
String query = "insert into myemp(empno,ename,sal,deptno) values(" + TextBox1.Text + ",'" +
TextBox2.Text + "'," + TextBox3.Text + "," + TextBox4.Text + ")";
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = query;
cmd.Connection = con;
cmd.ExecuteNonQuery();
Label1.Text = "data has been inserted";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}

SEARCHING RECORD FROM DATABASE

protected void Button1_Click(object sender, EventArgs e)


{
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
String myquery = "select * from myemp where empno=" + TextBox2.Text;
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
Label1.Text = "record found";
TextBox3.Text = ds.Tables[0].Rows[0]["ename"].ToString();
TextBox4.Text = ds.Tables[0].Rows[0]["sal"].ToString();
TextBox5.Text = ds.Tables[0].Rows[0]["deptno"].ToString();
}
else
{
Label1.Text = "record not found";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}
}

protected void Button2_Click(object sender, EventArgs e)


{
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
}

UPDATING RECORD IN DATABASE


protected void Button1_Click(object sender, EventArgs e)
{
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
String myquery = "select * from myemp where empno=" + TextBox1.Text;
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
con.Close();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
Label2.Text = "record found";
TextBox2.Text = ds.Tables[0].Rows[0]["ename"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["sal"].ToString();
TextBox4.Text = ds.Tables[0].Rows[0]["deptno"].ToString();
}
else
{
Label2.Text = "record not found";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
String updatedata="update myemp set
ename='"+TextBox2.Text+"',sal="+TextBox3.Text+",deptno="+TextBox4.Text+" where
empno="+TextBox1.Text;
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = updatedata;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Label2.Text = "";
Label1.Text = "data has been updated";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}

DELETING A RECORD FROM DATABASE

protected void Button2_Click(object sender, EventArgs e)


{
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
String myquery = "select * from myemp where empno=" + TextBox1.Text;
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = myquery;
cmd.Connection = con;
OracleDataAdapter da = new OracleDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
Label2.Text = "record found";
TextBox2.Text = ds.Tables[0].Rows[0]["ename"].ToString();
TextBox3.Text = ds.Tables[0].Rows[0]["sal"].ToString();
TextBox4.Text = ds.Tables[0].Rows[0]["deptno"].ToString();
Label2.Text = "";
}
else
{
Label2.Text = "record not found";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
}

protected void Button1_Click(object sender, EventArgs e)


{
String mycon = "data source=orcl;user id=scott;password=tiger;unicode=true";
String deldata = "delete from myemp where empno=" + TextBox1.Text;
OracleConnection con = new OracleConnection(mycon);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.CommandText = deldata;
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Label2.Text = "";
Label1.Text = "data deleted";
TextBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
Label2.Text = "";
}

You might also like