且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示通过sql select检索的数据

更新时间:2023-02-04 21:26:18

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



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



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..