【发布时间】:2014-06-21 06:16:04
【问题描述】:
我正在创建小型房地产网站,但需要一些帮助来显示数据库中的记录。
我想要这样的结果:-
这是我显示来自 2 个表的静态单条记录的代码:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class Result : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd,cmd1;
SqlDataReader dr;
string city1, area1, type1;
int min, max;
int id = 0;
int id2 = 0;
protected void Page_Load(object sender, EventArgs e)
{
city1 = Request.QueryString["city"];
area1 = Request.QueryString["area"];
type1 = Request.QueryString["propertytype"];
// type1= "1bhk";
min = Convert.ToInt32(Request.QueryString["minprice"]);
max = Convert.ToInt32(Request.QueryString["maxprice"]);
id = Convert.ToInt32(Request.QueryString["uid"]);
// id = 1;
con = new SqlConnection("integrated security=true; database=data1; server=sudhir-pc");
con.Open();
//cmd = new SqlCommand("select price,area,imagename,users_id from property where city='" + city1 + "' and area='" + area1 + "' and propertytype='" + type1 + "' and users_id=" + id + "", con);
// cmd1 = new SqlCommand("select frstname,laststname,contactno from users where users_id='"+id+"'", con);
cmd = new SqlCommand("select price,area,imagename,users_id,available from property where city=@city1 and area=@area1 and propertytype=@type1", con);
cmd.Parameters.AddWithValue("@city1",city1);
cmd.Parameters.AddWithValue("@area1",area1);
cmd.Parameters.AddWithValue("@type1",type1);
// cmd.Parameters.AddWithValue("@id",id);
dr = cmd.ExecuteReader();
while (dr.Read())
{
Label1.Text = (string)dr["price"].ToString();
Label2.Text= (string)dr["area"];
string imgstr=(string)dr["imagename"];
int id1 = (int)dr["users_id"];
Image1.ImageUrl = "~/upload/"+imgstr+"";
Label13.Text = city1;
Label14.Text = type1;
Label16.Text = (string)dr["available"].ToString();
id2 = (int)dr["users_id"];
}
dr.Dispose();
cmd1 = new SqlCommand("select firstname,laststname,contactno from users where users_id=" + id2 + "", con);
dr = cmd1.ExecuteReader();
while (dr.Read())
{
Label4.Text = (string)dr["firstname"];
Label5.Text = (string)dr["laststname"];
if (id > 0)
Label6.Text = (string)dr["contactno"];
}
if (id < 1)
Label6.Text = "To see contact information. Register!";
}
}
但我需要动态显示多条记录我应该使用哪个控件?
【问题讨论】: