如何在C#中打印标签中的所有行数据? [英] How to print all row data in label in C#?
本文介绍了如何在C#中打印标签中的所有行数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这很简单,但我不知道我是怎么做到的。我有一个表文章,有四行id,电子邮件,标题,数据。我想在一个标签的一行中打印这些数据。
我尝试过:
.aspx.cs
this is simple but i have no idea how i do that. i have a table "article" with four row "id","email","title","data". i want to print this data in the label mean for one row for one label.
What I have tried:
.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from data where id=@article_id", con);
cmd.Parameters.AddWithValue("@article_id", article_id.Text);
title.Text = cmd.ExecuteScalar().ToString();
con.Close();
}
.aspx
.aspx
<asp:Label runat="server" ID="title" Font-Bold="true"></asp:Label>
<asp:Label runat="server" ID="email" Font-Bold="true"></asp:Label>
<asp:Label runat="server" ID="title" Font-Bold="true"></asp:Label>
<asp:Label runat="server" ID="data" Font-Bold="true"></asp:Label>
推荐答案
SqlCommand cmd = new SqlCommand("select * from data where id=@article_id", con);
cmd.Parameters.AddWithValue("@article_id", article_id.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt!= null && dt.Rows.Count>0)
{
var row = dt.Rows[0];
lblID.Text = row["id"].ToString();
lblTitle.Text = row["title"].ToString();
lblData.Text = row["data"].ToString();
}
这篇关于如何在C#中打印标签中的所有行数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文