ADO.NET
It stands for Active X data objects. It is a set of classes used to interact with Data Sources like database, xml file etc.
Let's see with an example:
It stands for Active X data objects. It is a set of classes used to interact with Data Sources like database, xml file etc.
Let's see with an example:
select * from employeeMaster
This is table in our database.
Now Create an asp.net project in Visual studio.
Step 1 : In aspx page, let's take a gridview
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gdvEmlpoyee" runat="server"></asp:GridView>
</div>
</form>
</body>
</html>
Step 2 : put the following code in the code behind.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//Sql
server class
namespace ADONET
{
public partial class CreatingSqlConnection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=.
;database=employee;integrated security=SSPI");
SqlCommand sqlcmd = new SqlCommand("Select * from
EmployeeMaster", sqlcon);
sqlcon.Open();
SqlDataReader sqlreader = sqlcmd.ExecuteReader();
gdvEmlpoyee.DataSource = sqlreader;
gdvEmlpoyee.DataBind();
sqlcon.Close();
}
}
}
Step 3 : Press F5 or build website and view in browser. you will see the following output
Interaction Process
Using Try, Catch and finally while creating connection
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//Sql
server class
namespace ADONET
{
public partial class CreatingSqlConnection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection sqlcon = new SqlConnection("Data Source=.
;database=employee;integrated security=SSPI");
try
{
SqlCommand sqlcmd = new SqlCommand("Select * from
EmployeeMaster", sqlcon);
sqlcon.Open();
SqlDataReader sqlreader = sqlcmd.ExecuteReader();
gdvEmlpoyee.DataSource =
sqlreader;
gdvEmlpoyee.DataBind();
}
catch
{
}
finally
{
sqlcon.Close();
}
}
}
}
Using try and catch helps in catching any exception if occur in system.
finally will make sure that connection will be closed even if any exception occurs. So, it will helps in increasing the scalability of the system.
Using using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;//Sql
server class
namespace ADONET
{
public partial class CreatingSqlConnection : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection sqlcon = new SqlConnection("Data Source=.
;database=employee;integrated security=SSPI"))
{
SqlCommand sqlcmd = new SqlCommand("Select * from
EmployeeMaster", sqlcon);
sqlcon.Open();
SqlDataReader sqlreader = sqlcmd.ExecuteReader();
gdvEmlpoyee.DataSource =
sqlreader;
gdvEmlpoyee.DataBind();
sqlcon.Close();
}
}
}
}
Using is used to import namespace and closing connection properly.
0 comments:
Post a Comment