选择语句Visual Web Developer中的Case子句

时间:2013-05-30 19:01:02

标签: sql database if-statement case

我在获取所需输出方面遇到了一些麻烦。目前我有两个文本框,用于搜索数据库并显示结果(SerialNumber和SiteID)。现在只有当这两个框都被填满时,才会显示数据。但是,我想要的是如果序列号为空并且站点ID为3,则播放具有站点ID 3的所有数据。如果序列号为T7且站点ID为空,则显示序列号为T7的所有数据。如果两个字段都为空,则不显示任何内容最后,如果SerialNum为T7且SiteID为3,则显示T7和3的所有数据。

现在:当其中一个框为空时,数据库中不会显示任何内容。

以下是我在使用C#的.ASPX文件中使用的代码。任何人都可以对此代码提出建议以获得所需的输出,我想的可能是CASE条款或IF,但我不知道我会把它放在代码中的哪个位置?感谢

   <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb"   Inherits="_Default" %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0    Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

   <html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
   <title></title>
   </head>
   <body>
   <form id="form1" runat="server">
   <div>

    Enter Serial Number:<br />
    <asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Search" />
    <br />
    <br />
    Enter Site Name:<br />
    <asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>
    <br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server"></asp:Label>
    <br />
    <asp:Label ID="Label3" runat="server"></asp:Label>
    <br />
    <br />
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" BackColor="White" BorderColor="White" 
        BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" 
        DataSourceID="SqlDataSource1" GridLines="None" AllowSorting="True">
        <Columns>
            <asp:BoundField DataField="SerialNum" HeaderText="SerialNum" 
                SortExpression="SerialNum" />
            <asp:BoundField DataField="SiteId" HeaderText="SiteId" 
                SortExpression="SiteId" />
            <asp:BoundField DataField="Location" HeaderText="Location" 
                SortExpression="Location" />
            <asp:BoundField DataField="ContainerId" HeaderText="ContainerId" 
                SortExpression="ContainerId" />
            <asp:CheckBoxField DataField="Misplaced" HeaderText="Misplaced" 
                SortExpression="Misplaced" />
            <asp:CheckBoxField DataField="Frozen" HeaderText="Frozen" 
                SortExpression="Frozen" />
            <asp:CheckBoxField DataField="Scratched" HeaderText="Scratched" 
                SortExpression="Scratched" />
            <asp:CheckBoxField DataField="Destroyed" HeaderText="Destroyed" 
                SortExpression="Destroyed" />
            <asp:BoundField DataField="ScratchpoolId" HeaderText="ScratchpoolId" 
                SortExpression="ScratchpoolId" />
            <asp:BoundField DataField="Vendor" HeaderText="Vendor" 
                SortExpression="Vendor" />
            <asp:CheckBoxField DataField="Exported" HeaderText="Exported" 
                SortExpression="Exported" />
            <asp:CheckBoxField DataField="Erased" HeaderText="Erased" 
                SortExpression="Erased" />
            <asp:CheckBoxField DataField="Bad" HeaderText="Bad" SortExpression="Bad" />
            <asp:CheckBoxField DataField="Encrypted" HeaderText="Encrypted" 
                SortExpression="Encrypted" />
            <asp:CheckBoxField DataField="Virtual" HeaderText="Virtual" 
                SortExpression="Virtual" />
            <asp:BoundField DataField="ReturnDate" HeaderText="ReturnDate" 
                SortExpression="ReturnDate" />
            <asp:BoundField DataField="ReturnSiteId" HeaderText="ReturnSiteId" 
                SortExpression="ReturnSiteId" />
        </Columns>
        <FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
        <HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
        <PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
        <RowStyle BackColor="#DEDFDE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#594B9C" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#33276A" />
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:VerticesConnectionString %>" 

        SelectCommand="SELECT SerialNum, SiteId, Location, ContainerId, Misplaced, Frozen, Scratched, Destroyed, ScratchpoolId, Vendor, Exported, Erased, Bad, Encrypted,  Virtual, ReturnDate, ReturnSiteId FROM Media
                    FULL JOIN MediaInfo
                    ON Media.MediaID= MediaInfo.MediaID
                    FULL JOIN MediaStatus
                    ON MediaInfo.MediaID= MediaStatus.MediaID
                    FULL JOIN MediaReturn
                    ON MediaStatus.MediaID= MediaReturn.MediaID
                    FULL JOIN MediaGenT
                    ON Media.NoteID= MediaGenT.NoteID
    WHERE (([SerialNum] like '%' + @SerialNum + '%') AND ([SiteId] = @SiteId))">
        <SelectParameters>
            <asp:ControlParameter ControlID="TextBox1" Name="SerialNum"  PropertyName="Text" 
                Type="String" />
            <asp:ControlParameter ControlID="TextBox2" Name="SiteId"  PropertyName="Text" 
                Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

</div>
</form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

在这里你必须检查文本框中的参数 - 首先从主查询中删除where条件,根据提到的参数将其附加到where

SelectCommand=SELECT SerialNum, SiteId, Location, ContainerId, Misplaced, Frozen,    Scratched, Destroyed, ScratchpoolId, Vendor, Exported, Erased, Bad, Encrypted,  Virtual, ReturnDate, ReturnSiteId FROM Media
                FULL JOIN MediaInfo
                ON Media.MediaID= MediaInfo.MediaID
                FULL JOIN MediaStatus
                ON MediaInfo.MediaID= MediaStatus.MediaID
                FULL JOIN MediaReturn
                ON MediaStatus.MediaID= MediaReturn.MediaID
                FULL JOIN MediaGenT
                ON Media.NoteID= MediaGenT.NoteID"

if(SerialNumber!=null && SiteID!=null)
{

SelectCommand+="WHERE (([SerialNum] like '%' + @SerialNum + '%') AND ([SiteId] = @SiteId))"
}
elseif(SerialNumber.isnull() && !SiteId.isnull())
{
 SelectCommand+="WHERE  [SiteId] = @SiteId"
}
elseif(!SerialNumber.isnull() && SiteId.isnull())
{
SelectCommand+="where [SerialNum] like '%' + @SerialNum + '%'"
}

P.S请使用您所用语言的语法。