在代码编写中经常使用DataSet做为控件的数据源,通常一个控件只能绑定一个数据源。如果要将多次查询数据库的结果绑定到一个控件中,就必须把多次查询的结果填充到同一个数据源中。当然此功能也可以动态画表格来实现,不过我感觉动态处理表格实在是太麻烦了。为此做了以下测试并且试验成功,现在分享出来。
原理:将多次查询的结果填充到同一个DataSet的同一张Table中,不过两次填充时要将字段as成同样的别名,要不然就会出现不必要的NULL行。
代码如下:
如果已经有了两个相同列名的DataSet
ds1 跟 ds2
绑定到一个Grid
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
ds1.Merge(ds2);
原理:将多次查询的结果填充到同一个DataSet的同一张Table中,不过两次填充时要将字段as成同样的别名,要不然就会出现不必要的NULL行。
代码如下:
1
private void databing()
2
{
3
string sql1="select xqid as yhm ,xqmc as zsxm from xt_xq";//
4
string sql2="select xt_zdb_style as yhm ,description as zsxm from xt_zdb";//sql1 、sql2使用相同的字段别名yhm、zsxm
5
using (OracleConnection connection = new OracleConnection("Data Source=abeen;User Id=system;Password=abeen;"))
6
{
7
DataSet ds = new DataSet();
8
try
9
{
10
connection.Open();
11
OracleDataAdapter command = new OracleDataAdapter(sql2,connection);
12
command.Fill(ds,0,5,"TableName");//第一次填充数据到TableName
13
command = new OracleDataAdapter(sql1,connection);
14
command.Fill(ds,"TableName");//第二次填充数据到TableName
15
16
this.DataGrid1.DataSource=ds;//两次填充的结果都在ds的TableName中
17
this.DataGrid1.DataBind();
18
}
19
catch(System.Data.OracleClient.OracleException ex)
20
{
21
throw new Exception(ex.Message);
22
}
23![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
}
25
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
如果已经有了两个相同列名的DataSet
ds1 跟 ds2
绑定到一个Grid
DataSet ds1 = new DataSet();
DataSet ds2 = new DataSet();
ds1.Merge(ds2);