Académique Documents
Professionnel Documents
Culture Documents
NET PROGRAMMING
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
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 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.
All of the ADO.NET objects are implemented by classes in the System.Data namespace of the .NET Framework.
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 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
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
Creating ADO.NET
.NET Providers