Vous êtes sur la page 1sur 37

F5227 VISUAL BASIC .

NET PROGRAMMING

Topic 5.3 : Work with Data using ADO.NET

Course Learning Outcome (CLO)


Upon completion of this course, students should be able to : 1. Create a simple VB.NET based application based on the Windows Application template. 2. Describe on essential terminology including memory, data types and graphical user interface. 3. Apply object oriented programming techniques to create classes, add methods and add properties. 4. create a simple VB.NET based Web forms application that uses an XML Web Service and manipulate data in database by using Microsoft ADO.NET.

Course Learning Outcome:


Topic 5.0
1. create a simple VB.NET based Web forms application that uses an XML Web Service and manipulate data in database by using Microsoft ADO.NET

Topic 5.0
Topic 5.1 : Understand with Web Forms Topic 5.2 Create a web application using VB.NET Topic 5.3 : Work with Data using ADO.NET

Introduction
ADO.NET (ActiveX Data Objects .NET) is the primary data access API (Application Programming Interface) for the .NET Framework. ADO.NET is a data access technology from Microsoft .Net Framework , which provides communication between relational and nonrelational systems through a common set of components

ADO.NET
ADO.NET provides the classes that you use as you develop database applications with Visual Basic .NET as well as other .

ADO.NET consist of a set of Objects that expose data access services to the .NET environment. ADO.NET is built for disconnected architecture , so it enables truly disconnected Data Access and Data Manipulation through its Dataset Object, which is completely independent from the Data Source.

ADO.NET Architecture

How ADO.NET works?

ADO.NET uses two types of objects to access the data in a database:


data provider objects, which include data adapters, commands, and connections. datasets, which can contain one or more data tables

Data Providers
The .Net Framework includes mainly three Data Providers for ADO.NET and they are:
Microsoft SQL Server Data Provider OLEDB Data Provider ODBC Data Provider.

SQL Server uses the SqlConnection object , OLEDB uses the OleDbConnection Object and ODBC uses OdbcConnection Object respectively.

Data Providers
The ADO.NET classes that are responsible for working directly with a database are provided by the .NET data providers. These data providers include the classes you use to create data adapters, commands, and connections.

Data Providers
The Command Object uses to perform SQL statement or stored procedure to be executed at the Data Source The DataReader Object is a stream-based , forwardonly, read-only retrieval of query results from the Data Source, which do not update the data.

DataAdapter Object , which populate a Dataset Object with results from a Data Source .

The Connection Object provides physical connection to the Data Source.

The four Objects from the .Net Framework provide the functionality of Data Providers in the ADO.NET.

Data Adapter
To retrieve data from a database and store it in a data table, a data adapter object issues a Select statement thats stored in a command object. Next, the command object uses a connection object to connect to the database and retrieve the data. Then, the data is passed back to the data adapter, which stores the data in the dataset.

Command Object
To update the data in a database based on the data in a data table, the data adapter object issues an Insert, Update, or Delete statement thats stored in a command object. Then, the command object uses a connection to connect to the database and update the data.

Data Provider
The data provider remains connected to the database only long enough to retrieve or update the specified data. Then, it disconnects from the database and the application works with the data via the dataset object. This is referred to as a disconnected data architecture.

Disconnected data
One of the advantages of using a disconnected data architecture is improved system performance due to the use of fewer system resources for maintaining connections. Another advantage is that it makes ADO.NET compatible with ASP.NET web applications, which are inherently disconnected.

Dataset
A dataset stores data from the database so that it can be accessed by the application. The .NET data provider objects retrieve data from and update data in the database.

DataSet
DataSet provides a disconnected representation of result sets from the Data Source, and it is completely independent from the Data Source. DataSet provides much greater flexibility when dealing with related Result Sets.

DataSet
DataSet consists of a collection of DataTable objects that you can relate to each other with DataRelation objects. The DataTable contains a collection of DataRow and DataCoulumn Object which contains Data. The DataAdapter Object provides a bridge between the DataSet and the Data Source.

DataSet
The DataTable contains a collection of DataRow and DataColumn Object which contains Data.

Scenario
The data in a dataset is independent of the database that the data was retrieved from. In fact, the connection to the database is typically closed after the data is retrieved from the database. Then, the connection is opened again when its needed.

Because of that, the application must work with the copy of the data thats stored in the dataset. The architecture thats used to implement this type of data processing is referred to as a disconnected data architecture.

ADO.NET Connection String


Connection String is a normal String representation which contains Database connection information to establish the connection between Datbase and the Application. The Connection String includes parameters such as the name of the driver, Server name and Database name , as well as security information such as user name and password.

ADO.NET Connection String


Data providers use a connection string containing a collection of parameters to establish the connection with the database. The .NET Framework provides mainly three data providers: Microsoft SQL Server, OLEDB and ODBC.

All of the ADO.NET objects are implemented by classes in the System.Data namespace of the .NET Framework.

Microsoft SQL Server Connection String


Syntax: connectionString ="Data Source = ServerName; Initial Catalog = Databasename; User ID = UserName; Password=Password"

SQL Server Connection String


The SqlConnection Object is Handling the part of physical communication between the application and the SQL Server Database. An instance of the SqlConnection class in .NET Framework is supported the Data Provider for SQL Server Database.

SQL Connection
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connectionString As String Dim cnn As SqlConnection connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" cnn = New SqlConnection(connetionString) Try cnn.Open() MsgBox("Connection Open ! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

OLEDB Data Provider Connection String


Syntax: connectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = yourdatabasename.mdb;"

OLEDB 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 connetionString As String Dim cnn As OleDbConnection connetionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=yourdatabasename.mdb;"

cnn = New OleDbConnection(connetionString) Try cnn.Open() MsgBox("Connection Open ! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

ODBC Connection String


Syntax: connectionString = "Driver = {Microsoft Access Driver (*.mdb)}; DBQ = yourdatabasename.mdb;"

ODBC Connection
Imports System.Data.Odbc Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim cnn As OdbcConnection connetionString = "Driver={Microsoft Access Driver (*.mdb)};DBQ=yourdatabasename.mdb;" cnn = New OdbcConnection(connetionString) Try cnn.Open() MsgBox("Connection Open ! ") cnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class

Two ways to create ADO.NET objects


First, you can use the components in the Data tab of the Toolbox to create ADO.NET objects by dragging and dropping them onto a form. The second technique for creating ADO.NET objects is to write the code

Creating ADO.NET

.NET Providers

The SqlConnection class

Vous aimerez peut-être aussi