Sql Data Reader
It is used to read data in one direction and it is forward only means once the data is read by reader it cannot go back to that record. It is fastest way of reading data.
It requires connection to be established before accessing the data.
Instance of Sql data reader is created using Sql Command object ExecuteReader() method.
Let's see with an example:
It is used to read data in one direction and it is forward only means once the data is read by reader it cannot go back to that record. It is fastest way of reading data.
It requires connection to be established before accessing the data.
Instance of Sql data reader is created using Sql Command object ExecuteReader() method.
Let's see with an example:
select * from EmployeeTest
In the aspx file put the following code
In the aspx file put the following code
<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" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</div>
</form>
</body>
</html>
In code behind add the following code
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ADONET
{
public partial class SqlDataReaderAndNextResult : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String CS = ConfigurationManager.ConnectionStrings["EmplyoeeDB"].ConnectionString;
using (SqlConnection sqlcon = new SqlConnection(CS))
{
DataTable dt = new DataTable();
dt.Columns.Add("EmpID");
dt.Columns.Add("EmpName");
dt.Columns.Add("EmpDesignation");
dt.Columns.Add("EmpSalary");
SqlCommand sqlcmd = new SqlCommand("Select * from
EmployeeTest", sqlcon);
sqlcon.Open();
using (SqlDataReader sqlreader = sqlcmd.ExecuteReader())
{
while (sqlreader.Read())
{
DataRow dr = dt.NewRow();
if (sqlreader["EmpDesignation"].ToString().Equals("Software Engineer"))
{
dr["EmpID"] =
sqlreader["EmpID"];
dr["EmpName"] =
sqlreader["EmpName"];
dr["EmpDesignation"]
= sqlreader["EmpDesignation"];
dr["EmpSalary"] =
30000;
dt.Rows.Add(dr);
}
else
{
dr["EmpID"] =
sqlreader["EmpID"];
dr["EmpName"] =
sqlreader["EmpName"];
dr["EmpDesignation"]
= sqlreader["EmpDesignation"];
dr["EmpSalary"] =
40000;
dt.Rows.Add(dr);
}
}
gdvEmlpoyee.DataSource =
dt;
gdvEmlpoyee.DataBind();
sqlcon.Close();
}
}
}
}
}
Lets Execute our application we will get the following output
0 comments:
Post a Comment