Hi, Which one has more performance and speed? using dataset and
databinding asp.net controls or building string by SqlDataReader ?
the following is my two methods:
1. Using SqlDataReader :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="defau lt.aspx.cs" Inherits="homep age" %>
<html>
<body>
<%=HomepageNe ws %>
</body>
</html>
CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_ connectionStrin g);
SqlCommand command = connection.Crea teCommand();
command.Command Type = CommandType.Sto redProcedure;
command.Command Text = "GetTop5New s";
string news = "";
connection.Open ();
SqlDataReader reader = command.Execute Reader();
while (reader.Read())
{
news += @"<tr>
<td>" + reader["Title"].ToString() + @"</td>
<td rowspan=2>" + reader["Image"].ToString() + @"</
td>
</tr>
<tr>
<td>" + reader["Brief"].ToString() + @"</td>
</tr>";
}
reader.Close();
connection.Clos e();
return ("<table>" + news + "</table>");
}
2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="defau lt.aspx.cs" Inherits="homep age" %>
<html>
<body>
<form id="HomepageFor m" runat="server">
<asp:Repeater id="CommentsRep eater" runat="server">
<HeaderTemplate <table</HeaderTemplate>
<ItemTemplate >
<tr>
<td>
<%# Eval("Title") %></td>
<td rowspan="2">
<%# Eval("Image") %></td>
</tr>
<tr>
<td>
<%# Eval("Brief") %></td>
</tr>
</ItemTemplate>
<FooterTemplate </table</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
CodeBehinde:
----------------------
protected void Page_Load(objec t sender, EventArgs e)
{
CommentsRepeate r.DataSource = TopFiveNews();
CommentsRepeate r.DataBind();
}
private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_ connectionStrin g);
SqlCommand command = connection.Crea teCommand();
command.Command Type = CommandType.Sto redProcedure;
command.Command Text = "GetTop5New s";
SqlDataAdapter adapter = new SqlDataAdapter( command);
DataSet ds = new DataSet();
adapter.Fill(ds );
return ds;
}
databinding asp.net controls or building string by SqlDataReader ?
the following is my two methods:
1. Using SqlDataReader :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="defau lt.aspx.cs" Inherits="homep age" %>
<html>
<body>
<%=HomepageNe ws %>
</body>
</html>
CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_ connectionStrin g);
SqlCommand command = connection.Crea teCommand();
command.Command Type = CommandType.Sto redProcedure;
command.Command Text = "GetTop5New s";
string news = "";
connection.Open ();
SqlDataReader reader = command.Execute Reader();
while (reader.Read())
{
news += @"<tr>
<td>" + reader["Title"].ToString() + @"</td>
<td rowspan=2>" + reader["Image"].ToString() + @"</
td>
</tr>
<tr>
<td>" + reader["Brief"].ToString() + @"</td>
</tr>";
}
reader.Close();
connection.Clos e();
return ("<table>" + news + "</table>");
}
2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup ="true"
CodeFile="defau lt.aspx.cs" Inherits="homep age" %>
<html>
<body>
<form id="HomepageFor m" runat="server">
<asp:Repeater id="CommentsRep eater" runat="server">
<HeaderTemplate <table</HeaderTemplate>
<ItemTemplate >
<tr>
<td>
<%# Eval("Title") %></td>
<td rowspan="2">
<%# Eval("Image") %></td>
</tr>
<tr>
<td>
<%# Eval("Brief") %></td>
</tr>
</ItemTemplate>
<FooterTemplate </table</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
CodeBehinde:
----------------------
protected void Page_Load(objec t sender, EventArgs e)
{
CommentsRepeate r.DataSource = TopFiveNews();
CommentsRepeate r.DataBind();
}
private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_ connectionStrin g);
SqlCommand command = connection.Crea teCommand();
command.Command Type = CommandType.Sto redProcedure;
command.Command Text = "GetTop5New s";
SqlDataAdapter adapter = new SqlDataAdapter( command);
DataSet ds = new DataSet();
adapter.Fill(ds );
return ds;
}
Comment