ASP.NET中怎样让Label控件显示数据库中某条记录中指定的字段

最近在做一个项目,需要用一个Label控件来显示数据库中的商品价格的数字,不知道怎样把Label控件和数据库绑定,麻烦懂的高手指点指点,小弟在此谢谢了!

给你写个最简单的例子比如说页面上有三个TextBox控件!
//连接字符串
string ConString = ConfigurationManager.ConnectionStrings["DemoConnectionString"].ConnectionString.ToString();

protected void Page_Load(object sender, EventArgs e)
{
string sql = "select username,password,sex from TestDB wher id=1";//sql语句
SqlConnection con = new SqlConnection(ConString);
con.Open();//打开连接
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
TextBox1.Text = dr["username"].ToString();//texbox1的值
TextBox2.Text= dr["password"].ToString();//texbox2的值
TextBox3.Text= dr["sex"].ToString();//texbox3的值
}
con.Close();//关闭连接
}

注意:不管你是想获取那个字段的值,sql语句总重要!
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-04-19
用sql查询出你需要的商品价格的某几条记录

复给一个datatable或set
datatable = sql查询出的数据
----------------------------------
string test = string.emity;
string _price = string.emity;
public string price
{
get{return price ;}
set{price = value;}
}
for(int i = 0;i<datatable.rows.count;i++)
{
test += datatable.rows[i]["price"].tostring()
}

_price = test;

<asp:label text='<%Eval(# price)%>'></asp:label>
相似回答