显示数据库查询的结果 - 我需要绑定吗?

时间:2013-02-04 16:26:30

标签: c# asp.net .net

我正在显示我从数据库中获取的属性列表。 我习惯于在经典ASP中工作,只需创建一个与DB的连接,然后遍历结果并显示它们。

我的问题是,我应该在.NET中做同样的事情,还是应该将数据绑定到元素?我更喜欢遍历结果,因为我想在带有样式的div中显示结果。

这是我的代码:

       SqlConnection conn = new SqlConnection(
        "Data Source=xxxx;Initial Catalog=Rets_Full_local;Integrated Security=True");
        SqlDataReader rdr = null;

       try
       {
           conn.Open();

           // 3. Pass the connection to a command object
           SqlCommand cmd = new SqlCommand(
               "select * from RETS_Listings_full" +
               " WHERE 1 =1 " + sqlWhere
               , conn);

           rdr = cmd.ExecuteReader();

           String resultsDiv = "";

           while (rdr.Read())
           {
               String rdr_MLSNUM = (rdr["MLSNUM"]).ToString();
               String rdr_SYSID = (rdr["SYSID"]).ToString();
               String rdr_LISTPRICE = (rdr["LISTPRICE"]).ToString();
               String rdr_PropType = (rdr["PROPTYPE"]).ToString();
               String rdr_Beds = (rdr["beds"].ToString());
               String rdr_BathsFull = (rdr["BATHSFULL"].ToString());
               String rdr_BathsHalf = (rdr["BATHSHALF"].ToString());
               String rdr_AREA = (rdr["AREA"]).ToString();
               String rdr_STREETNUM = (rdr["STREETNUM"]).ToString();
               String rdr_STREETNAME = (rdr["STREETNAME"]).ToString();
               String rdr_City = (rdr["CITY"]).ToString();
               String rdr_View = (rdr["VIEWDESC"]).ToString();

               resultsDiv += "<div class='row-fluid'>";
               resultsDiv += "<div class='span4'>";
                resultsDiv += " <img src='http://www.hiltonheadrealestates.com/listings/Photo" + rdr_SYSID + "-1.jpeg' width='250px'>";
            resultsDiv += "</div>";
            resultsDiv += " <div class='span8' >";
                resultsDiv += "List Price:  $" + rdr_LISTPRICE +"<br />";
                resultsDiv += "Property Type: " + rdr_PropType +"<br />";
                resultsDiv += "Address : " + rdr_STREETNUM + " " + rdr_STREETNAME + "<br />";
                resultsDiv += "Location : " + rdr_City + "<br />";
                resultsDiv += "Area : " + rdr_AREA + "<br />";
                resultsDiv += "Bedrooms: " + rdr_Beds  + "<br />";
                resultsDiv += "Full Baths: " +rdr_BathsFull+    "<br />";
                resultsDiv += "Half Baths: " +rdr_BathsHalf+"<br />";
                resultsDiv += "View: " + rdr_View + "<br />";
            resultsDiv += "   </div>";
            resultsDiv += "   </div> <hr>";



           }

           // print results to div
           if (rdr == null || !rdr.HasRows)
           {
               div_Results.Text = "No Results - Please click <a href='/'>here</a> to search again";
           }
           else
           {
               div_Results.Text = resultsDiv;
           }
           // End print results to div

       }
       finally
       {
           // close the reader
           if (rdr != null)
           {
               rdr.Close();
           }

           // 5. Close the connection
           if (conn != null)
           {
               conn.Close();
           }
       }
   }

1 个答案:

答案 0 :(得分:1)

您应该使用可绑定控件。保留代码隐藏中的大部分业务逻辑(.aspx.cs文件)和aspx中的表示逻辑(.aspx文件)

Repeater是最简单的控件,用于显示集合并保持对标记的完全控制。

欢迎使用ASP.NET