24
BCA-Sem-IV Client Server – Unit-2 " " What is ADO .NET and its advantages o ADO.NET is the set of classes provided by the .NET Framework for managing connections to data sources. o “Organize process of frontend – Backend that transfer primary memory’s data to secondary memory and store as database / table is called ADO.NET/ Database connectivity.” o ADO.NET classes are organized into namespaces: o System.Data, o System.Data.OleDb, o System.Data.SqlClient etc… o Most applications need data access at some point of time making it a crucial component when working with applications. o ADO.NET Data access is making the application interact with a database, where all the data is stored. o It can be used by any programming language of dot net framework. o Different applications have different requirements for database access. o VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work with data on the Internet. o Using ADO.NET, clients can connect to a variety of different types of data sources. That is, by using ADO.NET, the particular data source used by an application can be changed without requiring extensive changes to application code. o When an application interacts with the database, the connection is opened to serve the request of the application and is closed as soon as the request is completed. o ADO.NET retrieves information from a data source and caches it locally. o Client applications manipulate the locally cached data and periodically reconnect to the data source to update it and to retrieve new data. o It uses XML for transmitting data to and from database and application. Advantages: o By keeping connections open for only a minimum period of time, ADO .NET saves system resources o ADO.NET provides an abstraction layer between the .NET client and the data sources so that .NET applications can be loosely coupled to their data sources. o It provides maximum security for databases and o Also, ADO.NET when interacting with the database uses XML and converts all the data into XML format for database related operations making them more efficient. o It improves overall system performance. o ADO.NET Does Not Depend On Continuously Live Connections. o Data Can Be Cached in Datasets. o Datasets are Independent of Data Sources. o XML has no runtime/transport requirements and requires no special code required to transport across the Internet Page 1 of 24

What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

Embed Size (px)

Citation preview

Page 1: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

What is ADO .NET and its advantages

o ADO.NET is the set of classes provided by the .NET Framework for managingconnections to data sources.

o “Organize process of frontend – Backend that transfer primary memory’s datato secondary memory and store as database / table is called ADO.NET/Database connectivity.”

o ADO.NET classes are organized into namespaces:o System.Data, o System.Data.OleDb, o System.Data.SqlClient etc…

o Most applications need data access at some point of time making it a crucialcomponent when working with applications.

o ADO.NET Data access is making the application interact with a database, where allthe data is stored.

o It can be used by any programming language of dot net framework.o Different applications have different requirements for database access. o VB.NET uses ADO .NET (ActiveX Data Object) as its data access and

manipulation protocol which also enables us to work with data on the Internet. o Using ADO.NET, clients can connect to a variety of different types of data

sources. That is, by using ADO.NET, the particular data source used by anapplication can be changed without requiring extensive changes to application code.

o When an application interacts with the database, the connection is opened to servethe request of the application and is closed as soon as the request is completed.

o ADO.NET retrieves information from a data source and caches it locally. o Client applications manipulate the locally cached data and periodically reconnect

to the data source to update it and to retrieve new data. o It uses XML for transmitting data to and from database and application.

Advantages:

o By keeping connections open for only a minimum period of time, ADO .NET savessystem resources

o ADO.NET provides an abstraction layer between the .NET client and the datasources so that .NET applications can be loosely coupled to their data sources.

o It provides maximum security for databases ando Also, ADO.NET when interacting with the database uses XML and converts all the

data into XML format for database related operations making them more efficient. o It improves overall system performance. o ADO.NET Does Not Depend On Continuously Live Connections.o Data Can Be Cached in Datasets.o Datasets are Independent of Data Sources.o XML has no runtime/transport requirements and requires no special code required to

transport across the Internet

Page 1 of 24

Page 2: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

The ADO.NET Data Architecture/Framework

Components of ADO.NET:

Data Access in ADO.NET relies on two main components:

o DataSet o Data Provider

Page 2 of 24

Page 3: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

DataSet:

o It is a virtual representation of database or its sub set in the form of tables havingrows and columns.

o It can be considered as a local copy of the relevant portions of the database. o The dataset is a disconnected, in-memory representation of data. o The DataSet is persisted in memory and the data in it can be manipulated and

updated independent of the database. o When the use of this DataSet is finished, changes can be made back to the central

database for updating. o The data in DataSet can be filled from any valid data source like Microsoft SQL

server database, an Oracle database or from a Microsoft Access database. o A DataSet provides the .NET client's representation of a set of data.

Data Provider

o It is a set of related components that work together to connects an applicationto a data source by providing and maintaining the connection to the database.

o It retrieves data from the data source and sends changes to that data back to the

data source.

o The particular data provider that an application uses depends upon the data source.

o The .NET Framework currently comes with two Data Providers: I. SQL Data Provider which is designed only to work with Microsoft's SQL

Server. II. OleDb DataProvider which allows us to connect to other types of databases

like Access and Oracle.

Each Data Provider consists of the following component classes:

Connection:

o The Connection class provides a connection to the database. o To interact with databases, you must have a connection to it.o A Connection object is used by the Command object to know which database to

execute command upon.o Microsoft Visual Studio .NET provides two types of Connection classes: the

SqlConnection class, which is designed specifically to connect to Microsoft SQLServer, and the OleDbConnection class, which can provide connections to awide range of database types like Microsoft Access and Oracle.

o The Connection class contains all of the information required to open aconnection to the database.

o The following are the general methods in the Connection class:

Page 3 of 24

Page 4: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

I. Open() - Opens the connection to our database II. Close() - Closes the database connection III. Dispose - Releases the resources on the connection object. Used to force

garbage collecting, ensuring no resources are being held after our connection is used. Incidentally, by using the Dispose method you automatically call the Close method as well.

Steps to connect to database:

o create a databaseo start writing connection string in vb.neto set the providero specify the data source.

Program to check the state of the database connection

Imports System.Data.OleDb

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim cnn As New OleDbConnection("Provider=microsoft.jet.oledb.4.0; data source=d:\login.mdb") cnn.Open() If cnn.State = ConnectionState.Open Then MsgBox("connection is open") Else MsgBox("connection is closed") End If cnn.Close() End SubEnd Class

Page 4 of 24

Page 5: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

Command:

o It is used to execute a command such as SELECT, INSERT, UPDATE andDELETE commands on the database.

o Interacting with databases means to specify which actions you want to occur.o A Command object uses CONNECTION object to know which database to act

upon. So it is dependent upon Connection object.o The Command object is represented by two corresponding classes:

o SqlCommand o OleDbCommand.

o Command objects are used to execute SQL commands to a database across adata connection.

o Command object provides following methods that are used to execute commands onthe database:

o ExecuteReader ( ) - Simply executes the SQL query against the database and returns rows. For Example SqlCommand.ExecuteReader()

o ExecuteNonQuery - Used whenever you work with SQL statements which does not return any value like insert, update, delete.

o ExecuteScalar - Returns a single value as an object from your database. Generally used with aggregate functions.

Dim cmd As New OleDbCommand("Select * from login", cnn)

Program to insert data into the database using Command Object

Imports System.Data.OleDbPublic Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Employee.mdb" Dim myConnection As New OleDbConnection(connectionstring) myConnection.Open() Dim query As String

query = "insert into EMPLOYEE_TABLE (EmployeeID, Name, Address) VALUES (111, 'pk', 'SK Patel')" Dim myCommand As New OleDbCommand(query, myConnection)

myCommand.ExecuteNonQuery() myConnection.Close() MsgBox("Records inserted")

Page 5 of 24

Page 6: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

End SubEnd Class

Page 6 of 24

Page 7: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

DataReader

o Many data operations require that you get only a stream of data for reading. o The DataReader object allows you to obtain the results of SELECT statement

from the Command object.o The DataReader object provides a forward-only and read-only data from a

database. o Unlike other components of the Data Provider, DataReader objects cannot be

directly instantiated. o Rather, the DataReader is returned as the result of the Command object's

ExecuteReader ( ) method. o The SqlCommand.ExecuteReader ( ) method returns a SqlDataReader object,

and the OleDbCommand.ExecuteReader ( ) method returns an OleDbDataReader object.

o Because only one row is in memory at a time, the DataReader improves system performance.

Dim dr As SqlDataReaderdr = myCommand.ExecuteReader()

Program to show working of DataReader

Imports System.Data.OleDbPublic Class Form1 Inherits System.Windows.Forms.Form Dim cn As OleDbConnection Dim cmd As OleDbCommand Dim dr As OleDbDataReader

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Employee.mdb;") cn.Open() cmd = New OleDbCommand("select * from EMPLOYEE_TABLE", cn) dr = cmd.ExecuteReader While dr.Read() TextBox1.Text = dr(0) TextBox2.Text = dr(1) TextBox3.Text = dr(2) ' loading data into TextBoxes by column index End While cn.Close() End Sub End Class

Page 7 of 24

Page 8: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

DataAdapter object

o It connects a DataSet to a data source.o It is a core class responsible for disconnected data architecture.o It fills a disconnected DataSet with data and performs update.o It is essentially the middleman between the database and a DataSet. o You need a data adapter because datasets do not maintain any active connection to

the database—they are disconnected from the database. o The data adapter is what actually applies your SQL statements to a database and

causes your datasets to fill with data. o To work with the SQL Server data provider, you use SQLDataAdapter objects, and

to work with ODBC data providers, you use OleDbAdapter objects.o The Data Adapter is used either to fill a DataSet with data from the database with its

Fill ( ) method and Update() to update the database.

Dim oledbAdapter As OleDbDataAdapteroledbAdapter = New OleDbDataAdapter("select * from login", connection)

o The DataAdapter provides four properties that represent database commands: SelectCommand : used to execute a select query on database. InsertCommand : used to execute a insert query on database. UpdateCommand : used to execute a update query on database. DeleteCommand: used to execute a delete query on database.

Program to show the functionality of Dataset, DataAdapter and Dataview anddatagrid

Imports SystemImports System.DataImports System.Data.SqlClient

Public Class DataGridForm Inherits System.Windows.Forms.Form

Private Sub btnLoadData_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnLoadData.Click

Dim connectionString As String = "DataSource=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\user.mdf;IntegratedSecurity=True;User Instance=True" Dim conn As New SqlConnection(connectionString) Dim ds As New DataSet() Dim cmdString As String = "SELECT * FROM article" Dim dataAdapter As New SqlDataAdapter(cmdString, conn) dataAdapter.Fill(ds, "article")

Page 8 of 24

Page 9: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

Dim dv As New DataView(ds.Tables("article")) dgDetails.DataSource = dv End SubEnd Class

ADO.NET Database access process:

o A connection object establishes the connection for the application with thedatabase.

o The command object provides execution of the command to the database. o If the command returns more than a single value, the Command object returns a

DataReader to provide the data. o The DataAdapter is used to fill the Dataset object. o The database can be updated using the command object or the DataAdapter.

Page 9 of 24

Page 10: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

DataSet Classes:

Class Description Important Properties\Methods

DataSet

o Represents a locally cached

subset of the total data stored

in a data source or sources

and provides a relational

programming model for

working with that data.

o Includes representations of

tables, constraints, and

relations between tables.

o A DataSet object can be filled

with data from multiple data

sources.

o Properties include:

o Tables — A collection of

DataTable objects.

o Relations — A collection of

DataRelation objects.

DataTable

o Represents a table of data

within a DataSet.

o A DataTable can be created

programmatically or as the

result of a database query.

o Typically, an application

accesses the data in a

DataSet by iterating through

the Rows collection of the

appropriate DataTable.

Properties include:

o Rows — A collection of

DataRow objects.

o Columns — A collection of

DataColumn objects.

DataRowRepresents a single record or row in

DataTable

DataColumnRepresents a column or field of

DataTable

DataRelation Represents the relationship between

Page 10 of 24

Page 11: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

different tables in a data set.

Constraint

Represents the constraints or

limitations that apply to a particular

field or column.

DataView:

o A DataView enables you to create different views of the data stored in a DataTable, a capability that is often used in data-binding applications.

o Using a DataView, you can expose the data in a table with different sort orders, and you can filter the data by row state or based on a filter expression.

o A DataView provides a dynamic view of data in the underlying DataTable: the content, ordering, and membership reflect changes as they occur.

o The dynamic capabilities of the DataView make it ideal for data-binding applications.

Page 11 of 24

Page 12: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

Description of 1-Tier and 2-Tier Web Applications

Here we will discuss the various architectures of Client-Server environments. Perhaps themost influential Client-Server environment is the Internet and its global users. Specifically,we will be discussing tier architecture, which is the breaking down of an application intological chunks that are called Tiers. Tiers can exist on the same computer and beconnected virtually or logically or on different machines.

1-Tier Architecture:

o 1-Tier Architecture is the simplest, single tier on single user, and is the equivalent ofrunning an application on a personal computer.

o All the required components to run the application are located within it. Userinterface, business logic, and data storage are all located on the same machine.

o They are the easiest to design, but the least scalable.

o Because they are not part of a network, they are useless for designing webapplications.

2 Tier Architecture:

o 2-Tier Architectures supply a basic network between a client and a server.

o For example, the basic web model is a 2-Tier Architecture. A web browser makes arequest from a web server, which then processes the request and returns thedesired response, in this case, web pages.

o This approach improves scalability and divides the user interface from thedata layers.

o The entire application must be updated because layers at server side aren’tseparated.

3-Tier Architecture

o 3-Tier Architecture is most commonly used to build web applications.

o In this model, the browser acts like a client, middleware or an application servercontains the business logic, and database servers handle data functions.

o This approach separates business logic from display and data.

o 3-Tier Architectures provide finer granularity, which provides more modules tochoose from as the application is separated into smaller functions.

o Usually 3-Tier Architecture begins as a 3-Tier model and can be expanded.

o It provides finer granularity. Granularity is the ability of a system, in this case,an application, to be broken down into smaller components or granules.

o The finer the granularity, the greater the flexibility of a system. It can also be referredto as a system’s modularity.

o This improves flexibility and scalability.

Page 12 of 24

Page 13: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

o One of the best examples of 3-Tier Architecture in web applications is the popularshopping-cart web application.

o The client tier interacts with the user through GUIs (Graphic User Interfaces) andwith the application and the application server.

o In web applications, this client tier is a web browser.

o In addition to initiating the request, the web browser also receives and displayscode in dynamic HTML (Hypertext Markup Language), the primary language of theWorld Wide Web.

o In a shopping cart web application, the presentation tier displays informationrelated to such services as browsing merchandise, purchasing, and shopping cartcontents.

o It calls database calls, all in the goal of providing a more sophisticated response.

o This layer glues the whole application together and allows different nodes tocommunicate with each other and be displayed to the user through the browser. It islocated in the application server.

o In 3-Tier Architecture, the business logic is pulled out from the presentation tierand, as its own layer; it controls an application’s functionality by performing detailedprocessing. For example, in our shopping cart example, this tier completes creditcard authorization and calculates things like shipping costs and sales tax. Thebusiness tier can also be considered the integration layer.

o Most web application frameworks follow the Model View Controller (MVC) whichseparates the user interface, the business rules and the data model.

o There can also be a data tier. It usually consists of database servers. It keeps dataneutral and independent from application servers or business logic. Giving data itsown tier also improves scalability and performance. As it grows, it is easily moved toanother, more powerful machine.

Benefits of 3-Tier Architectureo There are less maintenance nightmares as related components are loosely coupled.

If we make changes to one component then it will not affect other components.

o So It improves application maintainability.

o It reduces the amount of spaghetti code.

o 3-Tier model also make applications more readable.

o More Reusability of components

o The 3 tier allows Architecture to be vendor independent.

o It multiplies developer productivity

o Application development and maintenance is less costly.

o Application becomes more scalable as small application can be extended.

o 3-Tier Architectures make application more robust because there is no singlepoint of failure.

Page 13 of 24

Page 14: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

o Tiers function with relative independence. For example, if a business changesdatabase vendors, they just have to replace the data tier and. The business logictier and the presentation tier remain unchanged.

o Likewise, if the presentation layer changes, this will not affect the data layer. Adeveloper would have to pick through the entire application code to implement anychanges.

o They provide authentication and authorization to provide security.

o This allows the web server to restrict user access based on pre-determined criteria.

o Finally, N-Tier Architecture helps developers build web applications because itallows developers to apply their specific skill to that part of the program that bestsuits their skill set. Graphic artists can focus on the presentation tier, whileadministrators can focus on the database tier.

Common environment components for windows as well as web application

The following controls are common both in windows and web applications:

o Label: Label is a static place holder for the text. We use it to display the static text inconjunction with other controls like text box etc.

o Button: There is no more popular control in Visual Basic than buttons, with the possible exception of text boxes. Buttons are the plain controls that you simply click and release, the buttons you see everywhere in Visual Basic applications-usually just rounded rectangular. Buttons provide the most popular way of creating and handling an event in your code-every Visual Basic programmer is familiar with the button Click event. Buttons can be clicked with the mouse or with the Enter key if thebutton has the focus.

o Textbox: A textbox is used to accept input from the user or to display some text. Its main property is text and multiline. Some of its main methods are cut(), copy() andpaste() etc. below is shown a image of Textbox control.

Page 14 of 24

Page 15: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

o Listbox: It displays a list of items from which we can make a selection. We can select one or more than one items from the list. Below is shown the list box example:

o Checkbox: Checkboxes are also familiar controls—you click a checkbox to select it,and click it again to deselect it. When you select a checkbox, a check appears in it, indicating that the box is indeed selected. You use a checkbox to give the user an option, such as true/false or yes/no. The checkbox can be used where we need to select many options from a given place. Below is given the picture of a check box:

Page 15 of 24

Page 16: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

o Radio Button: o Radio buttons, also called option buttons, are similar to checkboxes—the

user can select and deselect them—except for two things: they are round where checkboxes are square, and you usually use radio buttons together in groups.

o In fact, that's the functional difference between checkboxes and radio buttons—checkboxes can work independently, but radio buttons are intended to workin groups.

o When you select one radio button in a group, the others are automatically deselected. For example, although you might use checkboxes to select items for your lunch or dinner, you would use radio buttons to let the user select oneof a set of exclusive options, such as the current day of the week.

o Panel: o You use panels controls to enclose other controls, and that's particularly important

with controls such as radio buttons, which operate in groups.You use panels togroup other controls, usually to divide a form into regions by function. For example,you may have a menu form that lets the user select drinks in one panel and whatthey want on their sandwich in another.

Page 16 of 24

Page 17: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

What Are Databases?o A database is similar to a data file in that it is a storage place for data. Like a data

file, a database does not present information directly to a user; the user runs an application that accesses data from the database and presents it to the user in an understandable format. There are mainly 02 kinds of databases:

o Flat file database o Relational database.

A flat file database is a collection of some files having some data for example there some excel files in a folder, we can call it a flat file database because it is storage of data insimple files.

A relational database is a collection of related files having primary key and foreignkeys. The RDBMS is made up of tables which are related together. The primary key in atable is usually the most important one—the one you might use to sort. The foreign keyusually represents the primary key in another table, which gives you access to that table inan organized way. For example, we might add a field called student ID to our student gradetable. That same field, student ID, may be the primary key in the school registrar'sdatabase table, which lists all students. In our table, then, the student ID field is a foreignkey, allowing us to specify individual records in the registrar's table.

COMPONENTS OF DATABASE

The following items are the components of a database and form the building blocks of a database. They store the data that we want to save in our database.

Data

Data is the most important component of database which is the piece of information storedin tables. All manipulation is done with the data. It is stored in tables and thendifferent queries are used to manage and manipulate the data. On can even

Page 17 of 24

Page 18: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

generate reports to show the results of manipulated data and see the data stored inthe database (i.e Tables...).

Tables

A table is a made up from rows and columns. For example, you may have a table that stores details of customers' names and addresses. Another table would be used to store details of parts and yet another would be used for supplier's names and addresses. It is thetables that make up the entire database and it is important that we do not duplicate data at all. Only keys would duplicate (and even then, on some tables - these would be unique).

Columns

Columns are similar to fields, i.e., individual items of data that we wish to store. A customer's name, the price of a part, the date of an invoice are all examples of columns. They are also similar to the columns found in spreadsheets (the A, B, C etc along the top). They store similar kind of data.

Rows

Rows are similar to records as they contain data of multiple columns (like the 1,2,3 etc in a spreadsheet). In databases, a row can be made up of as many or as few columns as you want. This makes reading data much more efficient - you fetch what you want.

Keys

Keys are used to relate one table for another. For example. A customer places an order for some parts. We need to store the customer's details, the parts ordered and the supplier of the parts. We have primary and foreign keys in our databases apart from some other keys.

Now that you've set up a database, how do you work with the data in that database? One popular way is to use Structured Query Language (SQL). You use SQL to set up a query,which, when applied to a database, typically returns a dataset of records that matched yourSQL query—for example, you may have asked for all students that got a grade of B or better. You can do a great many things with databases using SQL—you can insert new records, create new tables, get datasets that match specific criteria.

There are Four major components of database system :-1. Data: Data is the most important component of database which is the piece of information stored in tables.2. Harware: These are the data processing and displaying hardware like hard disk, monitoretc.3. Software: These are the application programs which process the database and display the result as per requirement.4. Users: These are the end users which require the data.

Page 18 of 24

Page 19: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

DATA BASE IMPLEMENTATION ( INSERT , UPDATE , DELETE , SELECTION)

What is SQL?

SQL is short for Structured Query Language and is a simple language that provides instructions for building and modifying the structure of databases and for modifying the datastored in the tables. The main commands used to modify and retrieve data are:

Select - Fetches data from the database table. Insert - Inserts one or more rows of data into database table. Update - Modifies existing row(s) of data of database table Delete - Deletes rows of data from database table.

Example Tables

In the subsequent text, the following 3 example tables are used: p Table (parts) s Table (suppliers) sp Table (suppliers & parts)

pno descr color

P1 Widget Blue

P2 Widget Red

P3 Dongle Green

sno name city

S1 Pierre Paris

S2 John London

S3 Mario Rome

sno pno qty

S1 P1 NULL

S2 P1 200

S3 P1 1000

S3 P2 200

INSERT Statement

The INSERT Statement adds one or more rows to a table. It has two formats:

INSERT INTO table-1 [(column-list)] VALUES (value-list)

Only the columns listed will be assigned values. Unlisted columns are set to null, so unlisted columns must allow nulls. The values from the VALUES Clause (first form) are assigned to the corresponding column in column-list in order.

INSERT INTO p (pno, color) VALUES ('P4', 'Brown')

Before After

pno descr color

P1 Widget Blue

P2 Widget Red

P3 Dongle Green =>

pno descr color

P1 Widget Blue

P2 Widget Red

P3 Dongle Green

P4 NULL Brown

Page 19 of 24

Page 20: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

UPDATE Statement

The UPDATE statement modifies columns in selected table rows. It has the following general format:

UPDATE table-1 SET set-list [WHERE predicate]

The optional WHERE Clause has the same format as in the SELECT Statement. The WHERE clause chooses which table rows to update as per some criteria. If it is missing, all rows in table-1 are updated.

UPDATE Example

UPDATE sSET name = 'Tony', city = 'Milan'WHERE sno = 'S3'

Before After

sno name city

S1 Pierre Paris

S2 John London

S3 Mario Rome

=>

sno name city

S1 Pierre Paris

S2 John London

S3 Tony Milan

DELETE Statement

The DELETE Statement removes selected rows from a table. It has the following general format:

DELETE FROM table-1 [WHERE criteria]

The optional WHERE Clause has the same format as in the SELECT Statement. TheWHERE clause chooses which table rows to delete. If it is missing, all rows in table-1 areremoved.

DELETE FROM sp WHERE pno = 'P1'

Before After

sno pno qty

S1 P1 NULL

S2 P1 200

S3 P1 1000

S3 P2 200

=>

sno pno qty

S3 P2 200

Page 20 of 24

Page 21: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

SELECT Statement Basics

The SQL SELECT statement queries data from tables in the database. The statement begins with the SELECT keyword. The basic SELECT statement has 3 clauses:

SELECT FROM WHERE

The SELECT clause specifies the table columns that are retrieved. The FROM clause specifies the tables accessed. The WHERE clause specifies criteria.

For example,

SELECT name FROM s WHERE city='Rome'

This query accesses rows from the table - s. It then filters those rows where the city columncontains Rome. Finally, the query retrieves the name column from each filtered row. Using the example s table, this query produces:

name

Mario

A detailed description of the query actions:

The FROM clause accesses the s table. Contents:

sno name city

S1 Pierre Paris

S2 John London

S3 Mario Rome

The WHERE clause filters the rows of the FROM table to use those whose city column contains Rome. This chooses a single row from s:

sno name city

S3 Mario Rome

The SELECT clause retrieves the name column from the rows filtered by the WHERE clause:

name

Mario

SELECT Clause

Page 21 of 24

Page 22: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

The SELECT clause is mandatory. It specifies a list of columns to be retrieved from the tables in the FROM clause. It has the following general format:

SELECT [ALL|DISTINCT] select-list

select-list is a list of column names separated by commas. The ALL and DISTINCT specifiers are optional.

DISTINCT specifies that duplicate rows are discarded. A duplicate row is when each corresponding select-list column has the same value.

o The default is ALL, which retains duplicate rows.

For example,

SELECT descr, color FROM p

The column names in the select list can be qualified by the appropriate table name:

SELECT p.descr, p.color FROM pA column in the select list can be renamed by following the column name with the new name. For example:

SELECT name supplier, city location FROM s

This produces:

supplier location

Pierre Paris

John London

Mario Rome

FROM Clause

The FROM clause always follows the SELECT clause. It lists the tables accessed by the query. For example,

SELECT * FROM s

WHERE Clause

The WHERE clause is optional. When specified, it always follows the FROM clause. The WHERE clause filters rows from the FROM clause tables. Omitting the WHERE clause specifies that all rows are used.

Following the WHERE keyword is a logical expression, also known as a predicate.

Page 22 of 24

Page 23: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

Comparison Operators

The = (equals) comparison operator compares two values for equality. Additional comparison operators are:

> -- greater than < -- less than >= -- greater than or equal to <= -- less than or equal to <> -- not equal to

For example, SELECT * FROM sp WHERE qty >= 200

sno pno qty

S2 P1 200

S3 P1 1000

S3 P2 200

Page 23 of 24

Page 24: What is ADO .NET and its advantages - KSV ELEARN CS - UNIT-2.pdfo VB.NET uses ADO .NET (ActiveX Data Object) as its data access and manipulation protocol which also enables us to work

BCA-Sem-IV Client Server – Unit-2 " "

Home assignment

1. What is ADO .NET and its advantages

2. Explain ADO.NET Architecture

3. Explain DataSet and its following objects

a. DataSet

b. DataTable

c. DataRow

d. DataColumn

e. DataRelation

f. Constraint

4. Explain Data Provider and its following objects

a. Connection Object

b. Command object

c. DataAdapter object

d. DataReader object

5. Explain DataView

6. Explain 1-Tier, 2-Tier, 3-Tier and N-Tier Architectures

7. What are Common environment components for windows as well as web

application? Explain each.

8. What Are Databases? What are its components? Explain each.

9. Explain Data base implementation (Insert, Update, Delete, and Selection) with

examples.

Page 24 of 24