This article provides information on programming with ADO.NET including DataSet,DataAdapter and DataReader
Introduction
ADO.NET was first introduced in version 1.0 of the .NET Framework and provided an extensive array of features to handle data either live,while connected to the database ,or when disconnected.. With the introduction of ADO.NET 2.0, you can have more features and easier programming model. Some of the newly added features include the capability to to bulk load large quantities of data from varieties of sources, to batch process updates to the database with fewer roundtrips back to the database server, to reuse the same live connection for multiple operations as well as to achieve asynchronous access to the database.
In this chapter,we will be focussing on connection with SQL Server or SQL Server Express. you can actually use ADO.NET to connect to Oracle or MySQL.In .NET 2 Framework, Microsoft only provides two set of classes to connect to 2 databases platform that are Oracle and SQL server. For MySQL, you can download the drivers specifically written for .NET from their official http://www.mysql.com/ website
Introducing SqlConnection, SqlCommand Object in ASP.NET
The connectionstring normally consists of the Server IPAddress, Database username and Database Password.
The SqlCommand object uses the SqlConnection object to execute Sql Queries. You can either use InLine SQL queries or stored procedures, or direct table access. If the SQL Query uses the SELECT clause, the result set it returns is usually stored in DataSet or DataReader object.
Using the DataReader object in ASP.NET
Below is the basic sample code on using SqlConnection, SqlCommand and SqlDataReader objects.
SqlConnection oConn;
SqlCommand oCmd;
SqlDataReader oDr;
try
{
oConn = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
oConn.Open();
oCmd = oConn.CreateCommand();
oCmd.CommandText = "Select Top 3 * From Customers";
oCmd.CommandType = CommandType.Text;
oDr = oCmd.ExecuteReader(CommandBehavior.CloseConnection);
while(oDr.Read())
{
Response.Write (oDr["CustomerID"].ToString() + " " + oDr["ContactName"].ToString() + "
");
}
}
finally {
oDr.Close();
oConn.Close();
}
Code Sample Binding DataReader with GridView in ASP.NET
<FORM ID="form1" RUNAT="server">
<DIV>
Bind with AutoGenerate Columns = true
<ASP:GRIDVIEW ID="GridView1" RUNAT="server" AUTOGENERATECOLUMNS="true">