Database Hierarchy (dealing with data adapter,
datasets by using [Link])
Overview of [Link]
[Link] provides consistent access to data sources such as Microsoft SQL Server, as well as data
sources exposed through OLE DB and XML. Data-sharing consumer applications can use [Link] to
connect to these data sources and retrieve, manipulate, and update data.
[Link] cleanly factors data access from data manipulation into discrete components that can be
used separately or in tandem. [Link] includes .NET Framework data providers for connecting to a
database, executing commands, and retrieving results. Those results are either processed directly,
or placed in an [Link] DataSet object in order to be exposed to the user in an ad-hoc manner,
combined with data from multiple sources, or remoted between tiers. The [Link] DataSet object
can also be used independently of a .NET Framework data provider to manage data local to the
application or sourced from XML.
The [Link] classes are found in [Link], and are integrated with the XML classes found in
[Link]. When compiling code that uses the [Link] namespace
[Link] - Database Connection
[Link] is also a part of the .NET Framework. [Link] is used to handle data access.
What is [Link]?
• [Link] is a part of the .NET Framework
• [Link] consists of a set of classes used to handle data access
• [Link] is entirely based on XML
• [Link] has, unlike ADO, no Recordset object
Create a Database Connection
We are going to use the Northwind database in our examples.
First, import the "[Link]" namespace. We need this namespace to work with Microsoft
Access and other OLE DB database providers. We will create the connection to the database in the
Page_Load subroutine. We create a dbconn variable as a new OleDbConnection class with a
connection string which identifies the OLE DB provider and the location of the database. Then we
open the database connection:
To specify the records to retrieve from the database, we will create a dbcomm variable as a new
OleDbCommand class. The OleDbCommand class is for issuing SQL queries against database tables:
Create a DataReader
The OleDbDataReader class is used to read a stream of records from a data source. A DataReader is
created by calling the ExecuteReader method of the OleDbCommand object:
Using [Link];
sub Page_Load
// Create a Database Command
dim dbconn,sql,dbcomm
dbconn=New OleDbConnection("Provider=[Link].4.0;
data source=" & [Link]("[Link]"))
[Link]()
// Create a Database Command
sql="SELECT * FROM customers"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=[Link]()
end sub
Close the Database Connection
Always close both the DataReader and database connection after access to the database is no
longer required:
[Link]()
[Link]()
Structure
How to Catch SQL Exceptions and log them
SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;");
try
{
[Link]();
}
catch (SqlException e)
{
[Link] log = new [Link]();
[Link] = "My Application";
[Link]([Link]());
if ([Link] != [Link])
[Link]("Connection was not opened.");
}
finally
{
[Link]();
}
Keep Exception Information Private
Attackers often use information from an exception, such as the name of your server, database, or
table to mount a specific attack on your system