遍历数据库表的cols [英] loop through database table's cols

查看:166
本文介绍了遍历数据库表的cols的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好.我有问题请帮帮我.
我的数据库表中有三个cols分别是col1,col2和col3.我希望当用户单击按钮时,for循环将这些列的值一个接一个地显示在网页上.有人可以告诉我该怎么做吗?我在C#中使用asp.net.预先感谢.

hi everybody.i have some problem please help me.
i have three cols in my database table named col1,col2,and col3. i want that when user clicks on a button the a for loop takes the values of these cols one by one and show it on the web page. can some body tell me how to do it. i m using asp.net with c#. thanks in advance.

推荐答案

嗨saifullahiit


步骤1:
进行数据库连接并创建命令以选择值(例如,从employee中选择* -将提供employee的所有值)

第二步:
使用任何数据绑定的控制(网格或数据列表或列表视图或重发器),然后将该数据绑定到该控制

像这样:

需要添加名称空间:
使用System.Data.Sqlclient;

SqlConnection con =新的SqlConnecton(在此处输入您的连接字符串");
SqlCommand cmd =新的SqlCommand(在此处提供查询");
数据集ds = cmd.ExecuteReader();
if(ds.tables.count> 0)
{
if(ds.tables [0] .rows.count> 0)
{
grid1.Datasource = ds.tables [0];
grid1.databind();
}
}



像这样您需要做的,如果您有任何疑问,请告诉我

谢谢
naresh.G
Hi saifullahiit


step1:
take database connection and create command for selecting the values like(select * from employee--this will give all the values of employee)

step2:
use any databound contorl(gird or datalist or list view or repeter) then bind that data to that contorl

like this:

need to add namespace:
Using System.Data.Sqlclient;

SqlConnection con=new SqlConnecton("give here your connection string");
SqlCommand cmd=new SqlCommand("give your query here");
Dataset ds=cmd.ExecuteReader();
if(ds.tables.count>0)
{
if(ds.tables[0].rows.count>0)
{
grid1.Datasource=ds.tables[0];
grid1.databind();
}
}



like this you need to do,if you have any doubts,let me know

thanks
naresh.G


希望这会有所帮助.
foreach(DataColumn col in ds.Tables[0].Columns)
{
  Console.WriteLine("{0} = {1}", column, column.DataType);
}



[您的问题不清楚.请改善您的问题.]



[Your question is not clear. Please Improve you questions.]


这篇关于遍历数据库表的cols的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆