This action might not be possible to undo. Are you sure you want to continue?
Most of today's applications need to interact with database systems to persist, edit or view data. In .NET, data access services are provided through ADO.NET components. ADO.NET is an object oriented framework that allows you to interact with database systems. We usually interact with database systems through SQL queries or stored procedures. The best thing about ADO.NET is that it is extremely flexible and efficient. ADO.NET also introduces the concept of disconnected data architecture. In traditional data access components, you made a connection to the database system and then interacted with it through SQL queries using the connection. The application stays connected to the DB system even when it is not using DB services. This commonly wastes valuable and expensive database resources, as most of the time applications only query and view the persistent data. ADO.NET solves this problem by managing a local buffer of persistent data called a data set. Your application automatically connects to the database server when it needs to run a query and then disconnects immediately after getting the result back and storing it in the dataset. This design of ADO.NE T is called disconnected data architecture and is very much similar to the connectionless services of HTTP on the internet. It should be noted that ADO.NET also provides connection oriented traditional data access services. Traditional Data Access Architecture http://www.programmersheaven.com/articles/faraz/lesson13_img1.jpg ADO.NET Disconnected Data Access Architecture http://www.programmersheaven.com/articles/faraz/lesson13_img2.jpg Another important aspect of disconnected architecture is that it maintains a local repository of data in the dataset object. The dataset object stores the tables, their relationships and their different constraints. The user can perform operations like update, insert and delete on this local dataset. The changes made to the dataset are applied to the actual database as a batch when needed. This greatly reduces network traffic and results in better performance. Different components of ADO.NET
Before going into the details of implementing data access applications using ADO.NET, it is important to understand its different supporting components or classes. All of the generic classes for data access are contained in the System.Data namespace.
Class DataSet DataTable DataRow DataColumn DataRelation Constraint
Description The DataSet is a local buffer of tables or a collection of disconnected recordsets A DataTable is used to contain data in tabular form using rows and columns. Represents a single record or row in a DataTable Represents a column or field of a DataTable Represents the relationship between different tables in a DataSet. Represents the constraints or limitations that apply to a particular field or column.
Oracle has also provides its classes (drivers) optimized for the Oracle DB System. SELECT * from emp selects all the fields of all the records from the table named 'emp' SELECT empno. OleDbDataAdapter SqlDataReader. using the database specific classes generally provides optimized performance however. Class SqlConnection. Microsoft itself has provided the specialized and optimized classes for their SQL server database system.Data. Similarly.authorId = author. ename from emp selects the fields empno and ename for all of the records from the table named 'emp' SELECT * from emp where empno< 100 selects all records from the table named 'emp' where the value of the field empno is less than 100 SELECT * from article.ADO. The names of these classes start with 'Sql' and are contained in the System. SQL SELECT Statement This query is used to select certain columns of certain records from one or more database tables. author where article.authorId selects all records from the tables named 'article' and 'author' that have the same value of the field authorId SQL INSERT Statement . Microsoft has also provided the general classes which can connect your application to any OLE supported database server. In fact. you can use OleDb classes to connect to SQL server or Oracle database. This means that different database system providers may provide classes (or drivers) optimized for their particular database system.NET also contains some database specific classes. OleDbConnection SqlCommand. OleDbParameter Represents SQL a query Description Represents a connection to the database system A class that connects to the database system. The name of these classes start with 'OleDb' and these are contained in the System.Data. fetches the records and f A stream that reads data from the database in a connected design Represents a parameter to a stored procedure A review of basic SQL queries Here we present a brief review of four basic SQL queries. OleDbCommand SqlDataAdapter. OleDbDataReader SqlParameter.SqlClient namespace.OleDb namespace.
NET. Access is used to demonstrate the OleDb databases. We will use MS SQL server and MS Access database systems to perform the data access tasks.eWhen a user "deletes" a record.NET Enough review and introduction! Let's start something practical. ename) values(101. A more safer method is to flag a field with an end date.SqlClient namespace. UPDATE emp SET enddate = GetNow(date) WHERE empno = 101 To remove this record from the users reach in future quieries. We will highlight the specific statements for these two using comments like: ' For SQL server . INSERT INTO emp(empno. UPDATE emp SET ename = 'Eric Gamma' WHERE empno = 1 01 updates the record whose empno field is 101 by setting its ename field to 'Eric Gamma' SQL DELETE Statement This query is used to delete existing record(s) from a database table. Select * FROM emp WHERE enddate = Null Performing common data access tasks with ADO. For SQL server. SQL Server is used because probably most of the time you will be using MS SQL server when developing . we will be using classes from the System.OleDb namespace. This is open to abuse an human error. DELETE FROM emp WHERE empno = 101 deletes the record whose empno field is 101 from the emp table y Note that its not good practice to allow users to actually delete records from your database.Data. there is nothing different in these two approaches for developers and only two or three statements will be different in both cases. Now we will build an application to demonstrate how common data access tasks are performed using ADO. what really happens is this. In fact. For Access we will be using classes from the System. 'John Gut tag') inserts a record in to the emp table and sets its empno field to 101 and its ename field to 'John Guttag' SQL UPDATE Statement This query is used to modify existing records in a database table. I.NET applications (And theres a free cut down version available from Microsoft).This query is used to insert a record into a database table.Data.
Dim dataAdapter As New SqlDataAdapter(commandString. filling the DataSet object with the results of the query through the Data Adapter Reading the records from the DataTables in the DataSets using the DataRow and DataColumn objects If the SQL command is UPDATE. The dataset will be updated through the data adapter Accepting to save the changes in the DataSet to the database Since we are demonstrating an application that uses both SQL Server and Access databases we need to include the following namespaces in our application: . INSERT or DELETE. conn) ' For Access Dim dataAdapter As New OleDbDataAdapter(commandString. The fields of the table 'Article' are Field Name artId title topic authorId lines dateOfPublishing String String Type (Primary Key)Integer The unique identifier for an article The title of an article Description Topic or Series name of the article like 'Multithreading in Java' or Unique identity of author Number of lines in the article The date the article was published (Foreign Key)Integer Integer Date The 'ProgrammersHeaven' database also contains a table named 'Author' with the following fields: Field Name authorId name String Type (Primary Key)Integer Name of the author Descript The unique identity of the author Accessing Data using ADO.NET Data access using ADO. The database will have a table named 'Article'.NET involves the following steps: y y y y y y y y y Defining the connection string for the database server Defining the connection (SqlConnection or OleDbConnection) to the database using a connection string Defining the command (SqlCommand or OleDbCommand) or command string that contains the query Defining the Data Adapter (SqlDataAdapter or OleDbDataAdapter) using the command string and the connection object Creating a new DataSet object If the SQL command is SELECT. conn) For the example code. we will be using a database named 'ProgrammersHeaven'.
Author's Note: Connection string details are vendor specific. the user id (uid) and the password (pwd).com Defining a Connection A connection is defined using the connection string. For SQL Server.Imports System. database=programmersheaven.OleDb database Imports System. where it resides." First of all we have defined the instance name of the server.Its good practice for you to create another admin user and not use the SA user ever again. These days when you install Sql server the installation forces you to think of a password for the SA (System Administrator) user . a connection is created like this: ' forSql Server Dim conn As New SqlConnection(connectionString) And for Access. pwd=nicecoding. This will help stop intruders breaking in to your database.OLEDB.connectionstrings. a connection is created like this: ' for MS Access Dim conn As New OleDbConnection(connectionString) . For SQL Server we have written the following connection string: ' forSql Server Dim connectionString As String = "server=P -III. which is "P-III" on our system.4." + _ "uid=phuser. For Access." + _ "data source = c:\programmersheaven. A good source of connection strings for different databases is http://www.0.Jet.SqlClient ' for SQL Server Let's now discuss each of the above steps individually ' for Acces s Defining the connection string The connection string defines which database server you are using.Data. we have written the following connection string: ' for MS Access Dim connectionString As String = "provider=Microsoft. This object is used by the Data Adapter to connect to and disconnect from the database. Next we defined the name of the database.Data.mdb" We have defined the provider of the access database. Then we have defined the data source which is the location of the target d atabase.DataImportsSystem. your user name and password and optionally the database name.
Defining the command or command string The command contains the query to be passed to the database. a data adapter is created like this: ' for MS Access Dim dataAdapter As New OleDbDataAdapter(commandString. " + _ "name. We will use the DataAdapter object for this purpose and call its Fill() method. The command string we have used in our application is: Dim commandString As String = "SELECT " + _ "artId. dateOfPublishing " + _ "FROM " + _ "article.authorId = article. Creating and filling the DataSet Finally. fetches the result of the query and stores it in a local dataset.authorId" We have passed a query to select all the articles along with the author's name. Of course you may want to use a simpler query. The Data Adapter stores your command (query) and connection. " + _ "article.authorId as authorId. As we mentioned earlier. author " + _ "WHERE " + _ "author. This is the step where the Data Adapter connects to . topic. conn) We have created a new instance of the Data Adapter and supplied it the command string and connection object in the constructor call. Using the connection and query the DaraAdapter connects to the database when asked. We will see the command object (SqlCommand or OleDbCommand) later in the lesson. we need to create an instance of the DataSet. For SQL Server. The DataSet object is created simply as: Dim ds As New DataSet() We need to fill the DataSet with the results from the query. conn) And for Access. a Data Adapter is created like this: ' forSql Server Dim dataAdapter As New SqlDataAdapter(commandString. We are using a command string. lines. a DataSet is a local and offline container of data. such as: Dim commandString As String = "SELECT * from article" Defining the Data Adapter We need to define the Data Adapter (SqlDataAdapter or OleDbDataAdapter).Here we have passed the connection string to the constructor of the connection object. title.
Tables("prog") The indexer we have used takes the name of the table in the DataSet and returns the corresponding DataTable object. We can use the tables Rows and Columns collections to access the data in the table. This is all we need to connect and fetch data from the database.Fill(ds. We have supplied it the dataset to fill and the name of the table (DataTable) in which the result of query is filled. Now the results of the query is stored in the dataset object in the prog table. which is an instance of the DataTable. We can get a reference to this table by using the indexer property of the DataSet object's Tables collection. "prog") We have called the Fill() method of dataAdapter object. dataAdapter.the physical database and fetches the result of the query. Dim dataTable As DataTable = ds. .