Lab 9-The C# Station ADO - Net Tutorial
Lab 9-The C# Station ADO - Net Tutorial
ADO.NET is an object-oriented set of libraries that allows you to interact with data
sources. Commonly, the data source is a database, but it could also be a text file, an
Excel spreadsheet, or an XML file. For the purposes of this tutorial, we will look at
ADO.NET as a way to interact with a database.
Data Providers
We know that ADO.NET allows us to interact with different types of data sources and
different types of databases. However, there isn't a single set of classes that allow you to
accomplish this universally. Since different data sources expose different protocols, we
need a way to communicate with the right data source using the right protocol. Some
older data sources use the ODBC protocol, many newer data sources use the OleDb
protocol, and there are more data sources every day that allow you to communicate with
them directly through .NET ADO.NET class libraries.
ADO.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. These libraries are
called Data Providers and are usually named for the protocol or data source type they
allow you to interact with. table 1 lists some well known data providers, the API prefix
they use, and the type of data source they allow you to interact with.
table 1. ADO.NET Data Providers are class libraries that allow a common way to interact with
specific data sources or protocols. The library APIs have prefixes that indicate which
provider they support.
Provider Name
ODBC Data
Provider
OleDb Data
Provider
Oracle Data
Provider
SQL Data
Provider
Borland Data
Provider
API
prefix
Odbc
OleDb
Oracle
Sql
Bdp
ADO.NET Objects
The SqlConnection Object
To interact with a database, you must have a connection to it. The connection
helps identify the database server, the database name, user name, password,
and other parameters that are required for connecting to the data base. A
connection object is used by command objects so they will know which
database to execute the command on.
when reading from or writing to the database. Additionally, the data adapter
contains command object references for SELECT, INSERT, UPDATE, and DELETE
operations on the data. You will have a data adapter defined for each table in
a DataSet and it will take care of all communication with the database for
you. All you need to do is tell the data adapter when to load from or write to
the database.
or
SqlConnection conn = new SqlConnection(
"Data Source=DatabaseServer;Initial Catalog=Northwind;User
ID=YourUserID;Password=YourPassword");
or
SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='|
DataDirectory|\MyCustomer.mdf';Integrated Security=True;User Instance=True");
Using a SqlConnection
The purpose of creating a SqlConnection object is so you can enable other
ADO.NET code to work with a database. Other ADO.NET objects, such as a
SqlCommand and a SqlDataAdapter take a connection object as a parameter.
The sequence of operations occurring in the lifetime of a SqlConnection are as
follows:
1.
2.
3.
4.
5.
class SqlConnectionDemo
{
static void Main()
{
// 1. Instantiate the connection
SqlConnection conn = new SqlConnection(
"Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
SqlDataReader rdr = null;
try
{
// 2. Open the connection
conn.Open();
// 3. Pass the connection to a command object
SqlCommand cmd = new SqlCommand("select * from Customers", conn);
//
// 4. Use the connection
//
// get query results
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
The line above is typical for instantiating a SqlCommand object. It takes a string
parameter that holds the command you want to execute and a reference to a
SqlConnection object. SqlCommand has a few overloads, which you will see in the
examples of this tutorial.
Querying Data
When using a SQL select command, you retrieve a data set for viewing. To
accomplish this with a SqlCommand object, you would use the ExecuteReader
method, which returns a SqlDataReader object. We'll discuss the
SqlDataReader in a future lesson. The example below shows how to use the
SqlCommand object to obtain a SqlDataReader object:
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
// 2. Call Execute reader to get query results
SqlDataReader rdr = cmd.ExecuteReader();
Inserting Data
To insert data into a database, use the ExecuteNonQuery method of the
SqlCommand object. The following code shows how to insert data into a
database table:
// prepare command string
string insertString = @"
insert into Categories
(CategoryName, Description)
values ('Miscellaneous', 'Whatever doesn''t fit elsewhere')";
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand(insertString, conn);
// 2. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
Updating Data
The ExecuteNonQuery method is also used for updating data. The following
code shows how to update data:
// prepare command string
string updateString = @"
update Categories
set CategoryName = 'Other'
where CategoryName = 'Miscellaneous'";
// 1. Instantiate a new command with command text only
SqlCommand cmd = new SqlCommand(updateString);
// 2. Set the Connection property
cmd.Connection = conn;
// 3. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
Deleting Data
You can also delete data using the ExecuteNonQuery method. The following
example shows how to delete a record from a database with the
ExecuteNonQuery method:
// prepare command string
string deleteString = @"
delete from Categories
where CategoryName = 'Other'";
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand();
// 2. Set the CommandText property
cmd.CommandText = deleteString;
// 3. Set the Connection property
cmd.Connection = conn;
// 4. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
return just the single value you need. The following example shows how to do
this with the ExecuteScalar method:
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand("select count(*) from Categories", conn);
// 2. Call ExecuteNonQuery to send command
int count = (int)cmd.ExecuteScalar();
Console.WriteLine();
Console.WriteLine("Number of Records: {0}", numberOfRecords);
/// <summary>
/// use ExecuteReader method
/// </summary>
public void ReadData()
{
SqlDataReader rdr = null;
try
{
// Open the connection
conn.Open();
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
// 2. Call Execute reader to get query results
rdr = cmd.ExecuteReader();
// print the CategoryName of each record
while (rdr.Read())
{
Console.WriteLine(rdr[0]);
}
}
finally
{
// close the reader
if (rdr != null)
{
rdr.Close();
}
// Close the connection
if (conn != null)
{
conn.Close();
}
}
}
/// <summary>
/// use ExecuteNonQuery method for Insert
/// </summary>
public void Insertdata()
{
try
{
// Open the connection
conn.Open();
// prepare command string
string insertString = @"
insert into Categories
(CategoryName, Description)
values ('Miscellaneous', 'Whatever doesn''t fit elsewhere')";
// 1. Instantiate a new command with a query and connection
SqlCommand cmd = new SqlCommand(insertString, conn);
// 2. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
finally
{
// Close the connection
if (conn != null)
{
conn.Close();
}
}
/// <summary>
/// use ExecuteNonQuery method for Update
/// </summary>
public void UpdateData()
{
try
{
// Open the connection
conn.Open();
// prepare command string
string updateString = @"
update Categories
set CategoryName = 'Other'
where CategoryName = 'Miscellaneous'";
// 1. Instantiate a new command with command text only
SqlCommand cmd = new SqlCommand(updateString);
// 2. Set the Connection property
cmd.Connection = conn;
// 3. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
finally
{
// Close the connection
if (conn != null)
{
conn.Close();
}
}
/// <summary>
/// use ExecuteNonQuery method for Delete
/// </summary>
public void DeleteData()
{
try
{
// Open the connection
conn.Open();
// prepare command string
string deleteString = @"
delete from Categories
where CategoryName = 'Other'";
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand();
// 2. Set the CommandText property
cmd.CommandText = deleteString;
// 3. Set the Connection property
cmd.Connection = conn;
// 4. Call ExecuteNonQuery to send command
cmd.ExecuteNonQuery();
}
finally
{
// Close the connection
if (conn != null)
{
conn.Close();
}
}
}
/// <summary>
/// use ExecuteScalar method
/// </summary>
/// <returns>number of records</returns>
public int GetNumberOfRecords()
{
int count = -1;
try
{
// Open the connection
conn.Open();
// 1. Instantiate a new command
SqlCommand cmd = new SqlCommand("select count(*) from Categories", conn);
// 2. Call ExecuteScalar to send command
count = (int)cmd.ExecuteScalar();
}
finally
{
// Close the connection
if (conn != null)
{
conn.Close();
}
}
return count;