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


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

Advertisements

W

Warren Tang

Hi

Why not use DataReader + Reapter? Like this:

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

Regards
Warren
 
Ad

Advertisements

B

bruce barker

building 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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top