Beruflich Dokumente
Kultur Dokumente
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: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
Tool 1/Machine1
Front end
Back end
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: *doesn t support centralized database *cannot withstand fr large amount of data *to overcome the disadvantages of I layered approach we use 2nd 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
Tool1/Machine1
Front End
Ex:visualbasic,powerbuilder,.net,java
Native Drivers
Ex:MSAccess,MSSqlserver,Oracle,DB2,mysql
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
Visual basic
ND1
ND2
ND3
ND4
oracle
Sql server
DB2
MYSQL
*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
VB
POWER BUILDER
.NET
JAVA
ND1
ND2
ND3
ND4
oracle To overcome this disadvantage we use 3 layered approach 3 LAYER APPROACH: Tool1/Machine1
Front End
Ex:visualbasic,powerbuilder,.net,java
Middle ware
Ex:OLEDB,ODBC,JDBC
Native drivers
Back end
Ex:MSAccess,MSSqlserver,Oracle,DB2,mysql
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 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 1 Property and option Connection string Type String Description Gets or sets the connection string value used to open connection to the database Gets the time to wait while trying to establich a connection before terminating the attempt and generating an error.default time is 15seconds Gets the name of the instance of the database server to which to connect Getsthe size(in bytes) of network packets used to communicate with an instance of the database server Indicates the state of the connection object, all the connection state values are available in connectionstate enumeration
Connectiontimeout
Int
Datasource
String
Packetsize
Int
Enum
Broken
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 2 Begin transaction() Close() Starts a database transaction Used to close an opened connection to the database.this is the preferred method of closing any open connection Creates and returns a command object associated with the respective connection class Returns schema information fr the data source of this connection Opens a database connection with property settings specified by the connectionstring
Createcommand()
4 5
Getschema() Open()
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:
Code:
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; 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"); } } }
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
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 Indicates (sets or gets ) the value of the command text.use command type lik Text:when command text is sql query
Enum
Stored procedure: when command text is stored procedure name Text:when command text is table name
Connection
Connection Used to set or get the connection object name to used class with the command object 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 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 Used to set or get group of sql queries to b executed with the help of T-sql transaction at database
Parameters
Collection
Transaction
Truncation class
Execution methods with command class 1 2 3 ExecuteNonQuery() ExecuteReader() ExecuteScalar() Check the following table fr description of the three methods
Comparison among Execution methods of command class: Srno 1 ExecuteNonQuery Will work with action queries only(create ,alter ,drop ,insert ExecuteReader Will work with action and nonaction queries (select) ExecuteScalar Will work with non-action queries tat contains aggregate
,update ,delete) 2 Returns the count of rows effected by the query Return type is int Return value is optional and can be assigned to an integer variable Returns the collection of rows selected by the query
functions Returns the first row and first column value of the query result Return type is object Return value is compulsory and should be assigned to a variable of required type
3 4
Return type is datareader Return value is compulsory and should be assigned to an another object datareader
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();
Design:
Code:
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; 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=PRIVE725BB27C9;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();
} } }
o/p:
*in the above example we are nt displaying any message to the user and user doesn t know whether record is deleted frm database or nt *to display the message to the user we can write the code like
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient;
InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection con=new SqlConnection("Data Source=PRIVE725BB27C9;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 using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; 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=PRIVE725BB27C9;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:
Prog to delete a record by using the parameterized query: Design z same as above Code:
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; 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=PRIVE725BB27C9;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:
Code:
using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; 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=PRIVE725BB27C9;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:
namespace wapplication { public partial class Form1 : Form public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { SqlConnection con=new SqlConnection("Data Source=PRIVE725BB27C9;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:
Code:
using using using using using using using using using System; System.Collections.Generic; System.ComponentModel; System.Data; System.Drawing; System.Linq; System.Text; System.Windows.Forms; System.Data.SqlClient;
namespace wapplication { public partial class Form1 : Form { SqlConnection con = new SqlConnection("Data Source=PRIVE725BB27C9;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;
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:
o/p: