如何在一个下拉列表中组合两个表的查询?

时间:2014-07-07 01:26:07

标签: asp.net

3分 65个帖子 如何在一个下拉列表中组合两个表的查询? 几秒钟前| LINK

您好..

我有两张桌子和两张网格视图。表格为gridview1的SCEMPR,表格为gridview2的表格为WSCEMPR。两个表都有相同的列。它只是不同的数据。这就是为什么我需要不同的gridview。

我有选择月份的下拉列表。每当我选择下拉列表时,两个网格视图中的记录都应根据选择进行更改。但我的问题是,我只能查询表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"));

2 个答案:

答案 0 :(得分:0)

使用UNION

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

然后,如果需要将其中的一些绑定到一个gridview而将其余部分绑定到另一个gridview,则可以根据“TableX”列解析该集。

答案 1 :(得分: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"));