using asp.net databinding controls is more efficient or building strings manually?

using asp.net databinding controls is more efficient or building strings manually?

Post by hp_198 » Sun, 21 Sep 2008 23:29:58


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;
}
 
 
 

using asp.net databinding controls is more efficient or building strings manually?

Post by Warren Tan » Sun, 21 Sep 2008 23:41:33

Hi

Why not use DataReader + Reapter? Like this:

<asp:SqlDataSource runat="server" ID="data" DataSourceMode="DataReader" ...
<asp:Repeater DataSourceID="data" ...

Regards
Warren

 
 
 

using asp.net databinding controls is more efficient or building strings manually?

Post by bruce bark » Mon, 22 Sep 2008 08:11:41

uilding a string directly is the most efficient (but probably not
noticeable). using a datareader directly is risky and can lead to memory
leaks (like your sample code doesn't release resources on an exception).

-- bruce (sqlwork.com)



XXXX@XXXXX.COM wrote: