asp.net - 在一个dropdownlist中,如何组合两个表的查询?

  显示原文与译文双语对照的内容
0 0

3 点 65如何将两个表中的查询合并到一个表中? 几秒 ago|LINK

我有两个表和两个 gridviews 。 gridview2的表SCEMPR和的表 WSCEMPR 。 两个表都具有相同的列。 只是数据不同,所以我需要不同的gridview 。

我有下拉列表来选择月份。 每当我选择dropdownlist时,gridviews中的记录都应根据选定内容进行更改。 但是我的问题是,我只能对表 SCEMPR ( 用于 gridview1 ) 进行查询。 如何对表WSCEMPR进行另一个查询? 我应该把它组合起来还是做一个不同的查询?

下面是我的代码。请帮忙。

页面:

<asp:DropDownList ID="ddl2" runat="server" Font-Names="Verdana" AutoPostBack="True" AppendDataBoundItems="False" Font-Size="10pt" Height="20px" Width="194px" OnSelectedIndexChanged="ddl2_SelectedIndexChanged" style="margin-left: 0px">
<asp:ListItem Text="SELECT MONTH" Value=""/>
</asp:DropDownList></td>

服务器端:

//query ddl for month
string queryddl2 ="SELECT DISTINCT CASE RIGHT(RTRIM(QMBSTEST.SCEMPR.SPMTH), 2) WHEN '01' THEN 'January' WHEN '02' THEN 'February' WHEN '03' THEN 'March' WHEN '04' THEN 'April' WHEN '05' THEN 'May' WHEN '06' THEN 'June' WHEN '07' THEN 'July' WHEN '08' THEN 'August' WHEN '09' THEN 'September' WHEN '10' THEN 'October' WHEN '11' THEN 'November' WHEN '12' THEN 'December' ELSE '' END as SPMTH FROM QMBSTEST.SCEMPR";
connection = new OdbcConnection(@"Dsn=as400;Uid=FATIN;Pwd=FATIN;");
OdbcDataAdapter oda2 = new OdbcDataAdapter(queryddl2, connection);
DataSet dset2 = new DataSet();
oda2.Fill(dset2);
ddl2.Items.Clear();
ddl2.DataSource = dset2;
ddl2.DataValueField ="SPMTH";
ddl2.DataTextField ="SPMTH";
ddl2.DataBind();
ddl2.Items.Insert(0, new ListItem("ALL MONTHS","ALL"));
时间:原作者:6个回答

0 0

使用联合

SELECT 'FirstTable',col1,col2 FROM Table1
UNION
SELECT 'SecondTable',col1,col2 FROM Table2

如果需要将它的中一部分绑定到一个 gridview,它的余部分则可以以基于'tablex'列解析该集。

原作者:
0 0

应该是这样的:

//FOR WSCEMPR
string queryddl1 ="SELECT DISTINCT CASE RIGHT(RTRIM(QMBSTEST.SCEMPR.SPMTH), 2) 
 WHEN '01' THEN 'January' WHEN '02' THEN 'February' WHEN '03' THEN 'March' 
 WHEN '04' THEN 'April' WHEN '05' THEN 'May' WHEN '06' THEN 'June' 
 WHEN '07' THEN 'July' WHEN '08' THEN 'August' WHEN '09' THEN 'September' 
 WHEN '10' THEN 'October' WHEN '11' THEN 'November' WHEN '12' THEN 'December' 
 ELSE '' END as SPMTH FROM QMBSTEST.WSCEMPR";
//FOR SCEMPR
string queryddl2 ="SELECT DISTINCT CASE RIGHT(RTRIM(QMBSTEST.SCEMPR.SPMTH), 2) 
 WHEN '01' THEN 'January' WHEN '02' THEN 'February' WHEN '03' THEN 'March' 
 WHEN '04' THEN 'April' WHEN '05' THEN 'May' WHEN '06' THEN 'June' 
 WHEN '07' THEN 'July' WHEN '08' THEN 'August' WHEN '09' THEN 'September' 
 WHEN '10' THEN 'October' WHEN '11' THEN 'November' WHEN '12' THEN 'December' 
 ELSE '' END as SPMTH FROM QMBSTEST.SCEMPR";
string qryForDropdown = queryddl1 +" UNION" + queryddl2;
connection = new OdbcConnection(@"Dsn=as400;Uid=FATIN;Pwd=FATIN;");
OdbcDataAdapter oda1 = new OdbcDataAdapter(queryddl1, connection);
OdbcDataAdapter oda2 = new OdbcDataAdapter(queryddl2, connection);
OdbcDataAdapter dDown = new OdbcDataAdapter(qryForDropdown, connection);
DataSet dset1 = new DataSet();
DataSet dset2 = new DataSet();
DataSet dsetDropD = new DataSet();
oda1.Fill(dset1);
oda2.Fill(dset2);
dDown.Fill(dsetDropD);
ddl2.Items.Clear();
ddl2.DataSource = dsetDropD;
ddl2.DataValueField ="SPMTH";
ddl2.DataTextField ="SPMTH";
ddl2.DataBind();
ddl2.Items.Insert(0, new ListItem("ALL MONTHS","ALL"));
原作者:
...