Hello
Everybody...
We are
back after holy festivals i.e. Navratri and Rama Navmi. I hope you are well and
I wish the same. Goddess Durga and God Rama bless you...
Today I
will explain you the most and powerful topic, which plays very main role in our
data centric application in ASP.net. I know you got it, yes I am going to
explain you the very most ‘ADO.NET’.
ADO.net
is playing the very important role in our data centric application. It provides
an API to access database systems as we program in our application. ADO (Actice
Data Objects). The .Net framework
contains several namespace to access the Database objects. Below are the few
one includes with ADO.NET...
System.Data.SqlClientContains classes for connecting to Microsoft SQL Server version 7.0 and above
System.Data.OleDb
Contains classes for connecting to a data source that has an OLE DB provider
System.Data.Odbc
Contains classes for connecting to a data source that has an ODBC driver
The System.Data.SqlClient namespace contains the following three classes:
SQLConnection:
The SqlConnection class represents an open connection to a Microsoft SQL Server database.
SqlCommand:
The
SqlCommand class represents a SQL statement or stored procedure.
SqlDataReader:
The SqlDataReader class represents the results
from a database query.The next section of this chapter goes into the details of using each of these classes.
Instead of SQL Server if you are using a Microsoft Access database, then you will have to use the classes from the System. Data.OleDbnamespace. the System.Data.OleDb namespace includes the following classes:
OleDbConnection:
The OleDbConnection class used for Access database connection.
OledDbCommand:
The OleDbCommand class represents a Microsoft Access statement or stored procedure.
OleDbDataReader:
The OleDbDataReader class represents the results from a database query.
Below are the code, shown how to create connection string and access it through application and use of SQL Client classes..
Write the below code in web.config..under configuration section
<connectionStrings>
<add name="connString"
connectionString="Server=local; uid=sa;
pwd=pass; Initial Catalog=ved"/>
connectionStrings>
Here is the .aspx.cs page code:
using System;
using
System.Configuration;
using
System.Data;
using
System.Linq;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.HtmlControls;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Xml.Linq;
using
System.Data.SqlClient;
using
System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
SqlConnection
dbConn;
protected void Page_Load(object
sender, EventArgs e)
{
if
(!IsPostBack)
{
try
{
//SqlConnection...
dbConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connString"].ToString());
lblMessage.Text = "Database server connected successfully..";
//SqlConnection...
SqlCommand
cmd = new SqlCommand("SELECT Name, Address FROM Customer");
dbConn.Open();
cmd.Connection = dbConn;
//Sql
Data Reader..
SqlDataReader
dr = cmd.ExecuteReader();
lblMessage.Text = "";
//clear the previous message..
while
(dr.Read())
{
//concatenate
the data...in Message Label
lblMessage.Text += dr["Name"].ToString();
}
dr.Close();
dbConn.Close();
}
catch
(Exception ex)
{
lblMessage.Text = "Error occured while connecting through Databsae
Server.." + ex.Message;
}
}
}
}
.aspx page.
<asp:Label ID="lblMessage"
runat="server">asp:Label>
Comments