使用datatable显示数据库记录 [英] displaying the database record using datatable
问题描述
我正在使用数据表显示数据库记录
在LatestNewsMaster表记录中如下
id主题链接有效
1缺陷邮件defectmail.aspx A
2账户费用accountsfee.aspx A
3 Latestenews页面Latestnews.aspx A
我的代码如下
string constr = ConfigurationManager.ConnectionStrings [ Reception2_DB]。ConnectionString;
使用(SqlConnection con = new SqlConnection(constr))
{
使用(SqlCommand cmd = new SqlCommand( 选择来自LatestNewsMaster的链接,其中有效<>'d'))
{
cmd.Connection = con;
使用(SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
label1.Text = dt.Rows [ 0 ] [ 0 ]。ToString()。修剪();
return dt;
}
}
}
但是当我在标签dsiplaying中运行时,标签1中只有firt recrod defectmail.aspx 。没有显示数据库中的所有三个链接记录。
来自我上面的代码我犯了什么错误。
我猜你没有错,除非我认为你误解了什么。
标签是显示单个单词或单行的方式...虽然您有表格形式的数据..即行和列...并且为此有一个名为GridView的控件...您可以使用它来显示您的所有数据,如下所示。例如,您只需在名为GVDetails
的页面上添加Gridview控件,然后将绑定名为dt
到下面的网格视图。
GVDetails.DataSource = dt;
GVDetails.DataBind();
您可以查看以下链接得到我正在谈论的整个想法。
Gridview数据绑定。
如果您需要更多帮助,可以提出任何问题点击此解决方案或任何下方的有问题或评论按钮。
希望它有所帮助。 :)
i am displaying the database record using datatable
In LatestNewsMaster table record as follows
id Subject Link Active
1 Defect Mail defectmail.aspx A
2 Accounts Fee accountsfee.aspx A
3 Latestenews page Latestnews.aspx A
My code as follows
string constr = ConfigurationManager.ConnectionStrings["Reception2_DB"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select Link from LatestNewsMaster where active <> 'd'"))
{
cmd.Connection = con;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
label1.Text = dt.Rows[0][0].ToString().Trim();
return dt;
}
}
}
But when i run in label dsiplaying only firt recrod defectmail.aspx in the label1. not displaying all the three link records from the database.
from my above code what is the mistake i made.
There is no mistake you are doing I guess, Except I think you misunderstood something.
Labels are the way to display a single word or single line ... while you have the data in the form of table.. i.e. Rows and Columns... and for that there is a control called as GridView... You can use that to display all of your data like below.. For example you just add a Gridview control on your page named asGVDetails
and than you will bind your DataTable which is named asdt
to that gridview like below.
GVDetails.DataSource = dt; GVDetails.DataBind();
You may have a look at the following link to get the whole idea I am talking about as well.
Gridview Databinding.
Further if you need more help you may ask any question by clicking "Have a Question or Comment" button below this solution or any.
Hope it helps. :)
这篇关于使用datatable显示数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!