H
hp_1981
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="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<%=HomepageNews %>
</body>
</html>
CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";
string news = "";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
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.Close();
return ("<table>" + news + "</table>");
}
2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<form id="HomepageForm" runat="server">
<asp:Repeater id="CommentsRepeater" 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(object sender, EventArgs e)
{
CommentsRepeater.DataSource = TopFiveNews();
CommentsRepeater.DataBind();
}
private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";
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="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<%=HomepageNews %>
</body>
</html>
CodeBehinde:
----------------------
public string HomepageNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";
string news = "";
connection.Open();
SqlDataReader reader = command.ExecuteReader();
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.Close();
return ("<table>" + news + "</table>");
}
2. Using DataSet and Repeater :
-------------------------------------------------------------
ASPX:
----------------------
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="default.aspx.cs" Inherits="homepage" %>
<html>
<body>
<form id="HomepageForm" runat="server">
<asp:Repeater id="CommentsRepeater" 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(object sender, EventArgs e)
{
CommentsRepeater.DataSource = TopFiveNews();
CommentsRepeater.DataBind();
}
private DataSet TopFiveNews()
{
SqlConnection connection = new
SqlConnection(_connectionString);
SqlCommand command = connection.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "GetTop5News";
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}