ado.net lab manaual

23
ADO.NET Introduction: * here to understand the role of ADO.NET first we look into some basic terms lik front end application ,front end tool back end application,back end tool etc *Front end Application:th e application with which end user will interact lik screens,forms,reports is known as front end application *Back end Application: the application where we store the end user data is known as back end application *Front end Tool: the software / tool used t o design the front end application is known as front end tool Ex:dbase,foxpro,ms access,power builder,d2k/oracle form,visual basics,.net,java etc *Back end tool:the software used to design back end application is known as backend tool Ex:dbase, foxpro,ms access,sqlserver,oracle,db2,mysql,Sybase etc Layered approach: *now we look into approach called layered application (this z nt N-Tier Architecture) I layered approach: In this method both front end& back end applications are designed using same tool & can be present only in single machine

Upload: nitish0549

Post on 06-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 1/23

ADO.NET

Introduction:

* here to understand the role of ADO.NET first we look into some basic terms lik frontend application ,front end tool back end application,back end tool etc

*Front end Application:the application with which end user will interact lik

screens,forms,reports is known as front end application

*Back end Application: the application where we store the end user data is known as

back end application

*Front end Tool: the software / tool used to design the front end application is known as

front end tool

Ex:dbase,foxpro,ms access,power builder,d2k/oracle form,visual basics,.net,java etc

*Back end tool:the software used to design back end application is known as backend

tool

Ex:dbase, foxpro,ms access,sqlserver,oracle,db2,mysql,Sybase etc

Layered approach:

*now we look into approach called layered application (this z nt N-Tier Architecture)

I layered approach:

In this method both front end& back end applications are designed using same tool &

can be present only in single machine

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 2/23

Tool 1/Machine1

Ex:Dbase,Foxpro,Msaccess

Advantages of I layer Approach:

*as front end & back end applications are designed using same tool no separate translator

mediums are required

Disadvantages of I layered approach:

*doesnt support centralized database

*cannot withstand fr large amount of data

*to overcome the disadvantages of I layered approach we use 2

nd

layered approach

2nd

layered approach:

* in this method both front end & back end applications are designed using two different tools & can b

present in two different machines

Front end

Back end

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 3/23

Tool1/Machine1

Ex:visualbasic,powerbuilder,.net,java

EX:DAO,RDO,ADO,ADO.NET

Ex:tns listener fr oracle Ex:MSAccess,MSSqlserver,Oracle,DB2,mysql

Sql native client fr sql server

Tool2/Machine2

Role of Native Drivers:

*as front end & back end applications are designed using 2 different tools.so,front end tool cannt

understand front end tool.Native drivers are used as translator between front end & back end tools

*front end tools native driver will take the request from front end tool,converts into back end tool

native driver understandable format & sends to front end

*Back end tool native driver will take the request from back end tool & converts into front end tool

native drivers understandable format & sends to front end

Advantages of 2nd

Layered approach:

*supports centralized database

*Most of the databases can withstand fr large amount of data

Disadvantages of 1st layered Aprroach:

*in any front end tool if one native driver is designed to interact with any backend tool & tat native

driver cannt be used to interact with other backend tool tat means every front end tool should hav

separate set of native driver to interact with back end tool lik

Front End

Native Drivers

Back End

Native Drivers

Back END

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 4/23

 

*if one native driver is designed fr any back end tool to interact with some front end tool tat native

driver cannt b used to interact with other front end tools.so every backend tool to hav one separate set

of native drivers to interact with each front end tool lik

To overcome this disadvantage we use 3 layered approach

3 LAYER APPROACH:

Tool1/Machine1

Ex:visualbasic,powerbuilder,.net,java

Visual basic

ND1

oracle

ND2

Sql server

ND3

DB2

ND4

MYSQL

VB

 

POWER

BUILDER

.NET JAVA

ND1 ND2 ND3 ND4

oracle

Front End

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 5/23

 

EX:DAO,RDO,ADO,ADO.NET

Ex:OLEDB,ODBC,JDBC

Ex:tns listener fr oracle Ex:MSAccess,MSSqlserver,Oracle,DB2,mysql

Sql native client fr sql server

Tool2/Machine2

In this method front end and back end tool will hav only one set of native drivers

*these native drivers do nt interact with each other, rather with 3rd component known as middle ware

*middle ware is designed in such a way tat it can understand any font end native drivers and any back

end native drivers

Abbreviations of the terms used:

DAO:Data access object

RDO:Remote data object

ADO:Active X data object

ADO.NET: :Active X data object fr .net

ODBC:open data base connectivity

JDBC:Java Data base connectivity

Data Providers I ADO.nET:

Native Drivers

Middle ware

Native drivers

Back end

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 6/23

*data providers in ADO.NET will help fr connecting to database,executing commands & retrieving results

from the database

*.net frame work data providers are light weight,creating a minimal layer between the data source and

code, increasing performance without sacrificing functionality

*all data providers or ADO.Net class library is a available in system.data name space

Working with connection object:

Introduction:

*connection object is used to establich connection to the database

*using connection object it is nt possible to perform any kind of operations lik inserting ,updating ,

deleting the records on database objects lik tables ,views ,stored procedures

Properties and methods with connection class:

Srno Property and option Type Description

1 Connection string String Gets or sets the connection string value used to open

connection to the database

2 Connectiontimeout Int Gets the time to wait while trying to establich a

connection before terminating the attempt and

generating an error.default time is 15seconds

3 Datasource String Gets the name of the instance of the database server to

which to connect

4 Packetsize Int Getsthe size(in bytes) of network packets used to

communicate with an instance of the database server

5 State

Close

Open

Connecting

Executing

Fetching

Enum Indicates the state of the connection object, all the

connection state values are available in connectionstate

enumeration

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 7/23

Broken

6 Serverversion String Gets a string tat contains the version of the instance of 

the database to which the client is connected

Methods with connection class

1 Begin transaction() Starts a database transaction

2 Close() Used to close an opened connection to the database.this is the

preferred method of closing any open connection

3 Createcommand() Creates and returns a command object associated with the respective

connection class

4 Getschema() Returns schema information fr the data source of this connection

5 Open() Opens a database connection with property settings specified by the

connectionstring

Steps to work with connection object:

Step1:declare connection object

Syntax:class name object name;

Ex:sqlconnection con;

Step2: define connection object

Syntax:object name=new class name (connection string);

Ex:con=new sqlconnection(connection string);

Step3:open the connection

Syntax:connection object name.open();

Ex:con.open();

Prog to connect to sqlserver using sqlclient provider:

Design:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 8/23

 

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication

{ public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con;con = new SqlConnection("Data Source=PRIVE-725BB27C9;Initial

Catalog=jo;Integrated Security=True");

con.Open();MessageBox.Show("connected succefully");

}

}

}

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 9/23

 

o/p:

Command object

Introduction:

*command object is used to perform the required operations on th database objects(tables,stored

procedure,views etc) lik inserting , deleting ,up dating records ,creating ,altering, dropping tables, stored

procedures ,views etc after establishing the connection.

*command object will work with connection oriented architecture

Properties and methods with command class:

Srno Property and

object

Type Description

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 10/23

1 Command text String Used to set or get the required command text value

,command text can b any one form sql query or store

procedure name or table name

2 Command type

Text(default)

Stored procedure

Table direct

Enum Indicates (sets or gets ) the value of the command text.use

command type lik

Text:when command text is sql query

Stored procedure: when command text is stored

procedure name

Text:when command text is table name

3 Connection Connection

class

Used to set or get the connection object name to used

with the command object

4 Command time out Int Used to set or get the wait time in seconds before

terminating the attempt to execute a command and

generating an error. Default time is 30 seconds

5 Parameters Collection Used to set or get the parameters names ,their values

required to b passed to the stored procedures of the

database or with the parameterized queries

6 Transaction Truncation

class

Used to set or get group of sql queries to b executed with

the help of T-sql transaction at database

Execution methods with command class

1 ExecuteNonQuery()

Check the following table fr description of the three methods2 ExecuteReader()

3 ExecuteScalar()

Comparison among Execution methods of command class:

Srno ExecuteNonQuery ExecuteReader ExecuteScalar

1 Will work with action

queries only(create

,alter ,drop ,insert

Will work with action and non-

action queries (select)

Will work with non-action

queries tat contains aggregate

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 11/23

,update ,delete) functions

2 Returns the count of 

rows effected by the

query

Returns the collection of rows

selected by the query

Returns the first row and first

column value of the query

result

3 Return type is int Return type is datareader Return type is object

4 Return value is optional

and can be assigned to

an integer variable

Return value is compulsory and

should be assigned to an another

object datareader

Return value is compulsory and

should be assigned to a variable

of required type

Steps to work with command object:

Step1: Declare command object

Syntax : class name object name;

Ex: sqlcommand cmd;

Step2: define command object

Syntax: object name:=new class ame(command text,connection object);

Ex:cmd =new sqlcommand (Delete empdetails where empid=105,con);

Step3: mention the command type

Syntax :commandobjectname . commandtype = commandtype.value;

Ex:cmd.commandtype=commandtype.text;

Step4:open the connection

Syntax: connection object name.open();

Ex:con.open();

Step5: Execute the command object

Syntax:command object name.ExecutionMethod();

Ex:cmd.ExecuteNonQUery();

Prog to connect to delete a record using sqlcommand class

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 12/23

Design:

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;string s = "Delete emp where empid=1";SqlCommand cmd;

cmd = new SqlCommand(s, con);cmd.CommandType = CommandType.Text;con.Open();cmd.ExecuteNonQuery();con.Close();

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 13/23

 }

}

}

o/p:

*in the above example we are nt displaying any message to the user and user doesnt know whether

record is deleted frm database or nt

*to display the message to the user we can write the code like

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {

public Form1(){

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 14/23

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;

string s = "Delete emp where empid=1";SqlCommand cmd;cmd = new SqlCommand(s, con);cmd.CommandType = CommandType.Text;con.Open();int i=cmd.ExecuteNonQuery ();con.Close();MessageBox.Show(i + "Records deleted");

}

}

}

o/p:

We can also write the above code using command class properties like:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 15/23

{public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;

string s = "Delete emp where empid=1";SqlCommand cmd=new SqlCommand ();cmd.CommandText = s;cmd.CommandType = CommandType.Text;cmd.Connection = con;con.Open();int i=cmd.ExecuteNonQuery ();

con.Close();MessageBox.Show(i + "Records deleted");

}

}

}

o/p:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 16/23

Prog to delete a record by using the parameterized query:

Design z same as above

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;

string s = "Delete emp where empid=@x";SqlCommand cmd=new SqlCommand (s,con);cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue("@x", textBox1.Text);con.Open();int i=cmd.ExecuteNonQuery ();con.Close();MessageBox.Show(i + "Records deleted");

}

}

}

o/p:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 17/23

 

Prog to delete a record by using the non-parameterized query:

Design:

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 18/23

using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;

string s="delete emp where empname='"+textBox1 .Text +"'";SqlCommand cmd=new SqlCommand (s,con);cmd.CommandType = CommandType.Text;con.Open();

int i=cmd.ExecuteNonQuery ();con.Close();MessageBox.Show(i + "Records deleted");

}

}

}

o/p:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 19/23

 

Desin is same

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {

public Form1(){

InitializeComponent();}

private void button1_Click(object sender, EventArgs e){

SqlConnection con=new SqlConnection("Data Source=PRIVE-725BB27C9;Initial Catalog=jo;Integrated Security=True");;

string s="delete emp where empname=@x";SqlCommand cmd=new SqlCommand (s,con);cmd.CommandType = CommandType.Text;

cmd.Parameters.AddWithValue("@x", textBox1.Text);con.Open();int i=cmd.ExecuteNonQuery ();con.Close();MessageBox.Show(i + "Record(s) deleted");

}

}

}

o/p:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 20/23

 

Prog to insert,update and delete records by using the non-parameterized query:

Code:

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;

using System.Linq;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;

namespace wapplication{

public partial class Form1 : Form {SqlConnection con = new SqlConnection("Data Source=PRIVE-

725BB27C9;Initial Catalog=jo;Integrated Security=True");SqlCommand cmd;public Form1(){

InitializeComponent();}

private void btninsrt_Click(object sender, EventArgs e){

string s = "insert into emp values('" + txtname.Text + "'," +txtid.Text + ",'" + txtdes.Text + "','" + txtdoj.Text + "'," + txtsal.Text +"," + txtdept.Text + ")";

cmd = new SqlCommand(s, con);cmd.CommandType = CommandType.Text;

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 21/23

con.Open();int i = cmd.ExecuteNonQuery();con.Close();MessageBox.Show(i + "Record(s) inserted");

}

private void btnupdate_Click(object sender, EventArgs e){

string s="update emp set empname='"+txtname .Text +"',empdesig='"+txtdes.Text +"',empdoj='"+txtdoj .Text +"',empsal="+txtsal .Text+",empdept='"+txtdept .Text +"'where empid="+txtid .Text +"";cmd = new SqlCommand(s, con);cmd.CommandType = CommandType.Text;con.Open();int i = cmd.ExecuteNonQuery();con.Close();MessageBox.Show(i + "Record(S) updated");

}

private void btndelete_Click(object sender, EventArgs e)

{string s="Delete emp where empid="+txtid .Text +"";cmd=new SqlCommand (s,con);cmd.CommandType =CommandType .Text ;con.Open();int i=cmd.ExecuteNonQuery ();con.Close ();MessageBox .Show (i+"Record deleted");

}

private void btnclear_Click(object sender, EventArgs e){

}

}

}

Design:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 22/23

 

o/p:

8/3/2019 Ado.net Lab Manaual

http://slidepdf.com/reader/full/adonet-lab-manaual 23/23