Awsome
Awsome
Awsome
NET provides a relatively common way to interact with data sources, but comes
in different sets of libraries for each way you can talk to a data source.
The following table outlines the four core objects that make up a .NET Framework data provider.
Object Description
Establishes a connection to a specific data source. The base class for all Connection
Connection
objects is the DbConnection class.
Executes a command against a data source. Exposes Parameters and can execute
Command within the scope of a Transaction from a Connection. The base class for all
Command objects is the DbCommand class.
Reads a forward-only, read-only stream of data from a data source. The base class
DataReader
for all DataReader objects is the DbDataReader class.
Populates a DataSet and resolves updates with the data source. The base class for
DataAdapter
all DataAdapter objects is the DbDataAdapter class.
In addition to the core classes listed in the table above, a .NET Framework data provider also
contains the classes listed in the following table.
Object Description
Enables you to enlist commands in transactions at the data source. The base class for
Transaction
all Transaction objects is the DbTransaction class.
Defines input, output, and return value parameters for commands and stored
Parameter
procedures. The base class for all Parameter objects is the DbParameter class.
SqlConnection Object
The first thing you will need to do when interacting with a data base is to create a
connection. The connection tells the rest of the ADO.NET code which data base it is talking to.
Connection String
Description
Parameter Name
Integrated Security Set to SSPI to make connection with user’s Windows login
Integrated Security is secure when you are on a single machine doing development. However,
you will often want to specify security based on a SQL Server User ID with permissions set
specifically for the application you are using. The following shows a connection string, using the
User ID and Password parameters:
Notice how the Data Source is set to DatabaseServer to indicate that you can identify a data base
located on a different machine, over a LAN, or over the Internet. Additionally, User ID and
Password replace the Integrated Security parameter.
SqlCommand Object
A SqlCommand object allows you to specify what type of interaction you want to perform with a
data base. For example, you can do select, insert, modify, and delete commands on rows of data
in a data base table.
SqlDataReader Object
A SqlDataReader is a type that is good for reading data in the most efficient manner possible.
You can *not* use it for writing data. SqlDataReaders are often described as fast-forward
firehose-like streams of data.
You can read from SqlDataReader objects in a forward-only sequential manner. Once you’ve
read some data, you must save it because you will not be able to go back and read it again.
A DataSet is an in-memory data store that can hold numerous tables. DataSets only hold data and
do not interact with a data source. It is the SqlDataAdapter that manages connections with the
data source and gives us disconnected behavior. The SqlDataAdapter opens a connection only
when required and closes it as soon as it has performed its task. For example, the
SqlDataAdapter performs the following tasks when filling a DataSet with data:
1. Open connection
2. Retrieve data into DataSet
3. Close connection
and performs the following actions when updating data source with DataSet changes:
1. Open connection
2. Write changes from DataSet to data source
3. Close connection
In between the Fill and Update operations, data source connections are closed and you are free to
read and write data with the DataSet as you need. These are the mechanics of working with
disconnected data.
There isn’t anything special about instantiating a DataSet. You just create a new instance, just
like any other object:
The DataSet constructor doesn’t require parameters. However there is one overload that accepts
a string for the name of the DataSet
Creating A SqlDataAdapter
The SqlDataAdapter holds the SQL commands and connection object for reading and writing
data. You initialize it with a SQL select statement and connection object:
As indicated earlier, the SqlDataAdapter contains all of the commands necessary to interact with
the data source.
namespace ConsoleApplication1
{
class Program
{
DataSet dataset = new DataSet();
static void Main(string[] args)
{
try
{
oConnection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(“select * from tblUser”,
oConnection);
adapter.Fill(dataset,“TEST”);
oConnection.Close();
}
catch (SqlException oSqlExp)
{
Console.WriteLine(“” + oSqlExp.Message);
}
catch (Exception oEx)
{
Console.WriteLine(“” + oEx.Message);
}
finally
{
if (oConnection != null)
{
oConnection.Close();
}
}
}