Professional Documents
Culture Documents
NET
What is ADO.NET?
• Fast
• XML support
• Scalability
• Disconnected Architecture
When learning a technology one should always be aware of both the advantages and the
disadvantages of using it. The following are the disadvantages of this architecture:
• DataProvider
• DataSet
DataProvider
A DataProvider encapsulates the protocols that ADO.NET uses to interact with different
types of databases. The following are the supported DataProviders:
• Odbc Data Provider
• Connection
• Command
• DataReader
• DataAdapter
• IDbConnection
• IDataReader
• IDbCommand
• IDbDataAdapter
Connection
The Connection object establishes a connection to the database using the user name,
password and the database name as parameters. The following are the supported
Connection classes:
• OleDbConnection
• OdbcConnection
• OracleConnection
• SqlConnection
Command
The Command object is used to send the SQL Statements to the database. Commands are
used to insert data, retrieve data and execute stored procedures in the database. The
following are the methods of the Command class:
• ExecuteScalar
• ExecuteReader
• ExecuteNonQuery
• ExecuteXmlReader
The ExecuteScalar method returns a single value from a query. This is faster compared
to using ExecuteReader and is the preferred choice to retrieve a single value from a
database table. The ExecuteReader method returns a single record at a time.
ExecuteNonQuery is used to change the state of a table. It can be used to insert, update
or delete records from the table. ExecuteNonQuery can also be used to execute stored
procedures. The ExecuteXmlReader method returns an XmlReader object. The
ExecuteNonQuery method returns an integer representing the number of rows affected in
the operation.
DataReader
A DataReader is a connected, forward only, read only stream of data that is retrieved
from the database. The DataReader requires an open connection so it works in the
connected mode. A DataReader is much faster compared to the disconnected DataSet,
but it does require an open available connection.
DataAdapter
The DataAdapter is a bridge between the DataSet and the underlying database. It
provides a set of methods and properties to move data to and fro between a database and
its in-memory representation, the DataSet. It is also used to update the data in the
database with the data in the DataSet. The following are two of the most important
methods of the DataAdapter class:
• Fill
• Update
The DataAdapter uses the Fill method to populate a DataSet or a DataTable object with
data from the database. The Update method of the DataAdapter class commits the
changes back to the database.
• InsertCommand
• UpdateCommand
• DeleteCommand
DataSet
• Disconnected
• XML support
Data View
A DataView is a class that provides a customized view of the DataSet. It is typically used
to sort or filter the rows.
Transactions
A transaction is a block of statements that guarantees that all or none of the statements in
the block are executed. In ADO.NET there are separate classes for using transactions,
one for each type of Connection. A transaction can be started by using the method
BeginTransaction on the currently active Connection. This method returns a Transaction
object. To commit the transaction, the method CommitTransaction is used. In order to
abandon the transaction, the method Rollback is executed. Transactions can be of the
following two types:
• Database transactions
The following code examples make use of the classes discussed above and show how one
can use the ADO.NET for efficient data storage and retrieval in a managed environment.
catch (Exception e)
{
sqlTransaction.Rollback();
}
finally
{
sqlConnection.Close();
}
The following code shows how we can use the Update() method of the DataAdapter class
to update a record.
Suggested Readings
Please refer to the following links for further references on this topic
http://www.startvbdotnet.com/ado/default.aspx
http://www.vbip.com/books/1861005563/chapter_5563_06.asp
Conclusion