我们如何在单个gridview中显示两个表 [英] how we can display two tables in a single gridview

查看:116
本文介绍了我们如何在单个gridview中显示两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单个网格视图中显示两个具有不同对象名称的表。

i want to display two tables in a single grid view which are having different object names.

SqlConnection con = new SqlConnection(ConnectionString);

String query= "select *from Employee1 ; select *from Employee2";

SqlCommand cmd = new SqlCommand(query,con);

SqlDataReader dr;

con.Open();

dr = cmd.ExecuteReader();

GridView1.DataSource = dr;

GridView1.DataBind();

con.Close();

推荐答案

将两个查询组合在一起,否则将SqlAdapter与数据集一起使用。

Combine Two Query in One otherwise use SqlAdapter with Dataset.
SqlConnection con = new SqlConnection(ConnectionString);

    String query = "select * from Employee1 , Employee2";

    SqlCommand cmd = new SqlCommand(query,con);

    SqlDataReader dr;

    con.Open();

    dr = cmd.ExecuteReader();

    GridView1.DataSource = dr;

    GridView1.DataBind();

    con.Close();


如果它们之间存在关系,或者在两个表之间分配相同数据,则需要在查询中连接表。你可以使用UNION。



欲了解更多信息,请参阅

- 加入基础知识 [ ^ ]

- UNION [ ^ ]



如果您愿意,还可以获取多个结果集。看一下对SQL Server执行多个SQL语句 [ ^ ]
You need to either join the tables in the query if there's a relationship between them or if the 'same' data is distributed between two tables you can use UNION.

For more information see
- Join Fundamentals[^]
- UNION[^]

Also you can fetch multiple results sets if you like. Have a look at Executing multiple SQL statements as one against SQL Server[^]


使用 SqlDataAdapter DataSet DataSet 可以包含多个表。
Use SqlDataAdapter with DataSet. DataSet can contain multiple tables.


这篇关于我们如何在单个gridview中显示两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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