如何显示通过sql select检索的数据 [英] How to show data retrieved thro sql select

查看:105
本文介绍了如何显示通过sql select检索的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以从sql中的表中选择数据。现在我想用c#使用gridview或任何其他方法显示相同的结果。以下是我的选择声明:



I am able to select data from a table in sql. Now I want to show the same result using c# using gridview or anyother means. Here is my select statement:

SELECT YEAR(exp_date) AS [Year],MONTH(exp_date) AS [Month],COUNT(*) from txfile where publishstatus='Y' Group By YEAR(exp_date), MONTH(exp_date)



如果我使用数据集,我想它只能使用表来完成。

任何人都可以用代码指导我。



问候。


If I use dataset I suppose it could be done only using a table.
Can anyone guide me with code.

Regards.

推荐答案

您可以尝试使用此代码从各自的数据库中选择和显示数据...



首先获取一个数据表来存储数据库中的表



You can try this code to select and display data from your respective database...

first of all get a data table to store a table from database

System.Data.DataTable dt=new System.Data.DataTable();



然后得到一个数据适配器,它将从数据库中选择一个表..

这个 DataAdapter 将采用 SELECT 查询字符串和一个连接字符串作为参数。




then get a data Adapter which will select a table from database..
this DataAdapter will take SELECT query string and a connection string as arguments.

System.Data.SqlClient.SqlDataAdapter da ;

da=new System.Data.SqlClient.SqlDataAdapter("select query","connection string");





将您的SELECT查询替换为查询和相应数据库的连接字符串代替上述功能中的连接字符串;



那么你将得到整个 DataAdapter da 中的表格。



稍后填写 DataTable dt 使用以下语法;





put your SELECT query in place of "query" and Connection String of respective database in place of "connection string" in above functionality;

then you will get whole table in DataAdapter da.

Later on fill the DataTable dt using following syntax;

da.fill(dt);





整张表现在将存储在 DataTable dt。



现在你可以通过访问它的索引来直接使用这个 dt

(例如。 dt.Rows [行数] [列数] .toString()



或者你可以按如下方式填写GridView:



DataTable dt 作为数据源提供给 Gridview





whole table will now be stored in DataTable dt.

Now you can use this dt directly by accessing its index
(eg. dt.Rows[index of rows][index of columns].toString())

or you can fill GridView as follows:

give DataTable dt as data source to Gridview

GridView1.datasource=dt;

现在我们需要使用gridview绑定数据,为此后使用

now we need to bind data with gridview, for that use following

GridView.DataBind();



表格现在将显示在GridView中..


Table will now be displayed in GridView..


这篇关于如何显示通过sql select检索的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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