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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hp_1981@yahoo.com

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

    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;
    }
  • Warren Tang

    #2
    Re: using asp.net databinding controls is more efficient or buildingstrings manually?

    Hi

    Why not use DataReader + Reapter? Like this:

    <asp:SqlDataSou rce runat="server" ID="data" DataSourceMode= "DataReader " ...
    <asp:Repeater DataSourceID="d ata" ...

    Regards
    Warren

    Comment

    • bruce barker

      #3
      Re: using asp.net databinding controls is more efficient or buildingstrings manually?

      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)



      hp_1981@yahoo.c om wrote:
      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;
      }

      Comment

      Working...