0

我在获得所需的输出时遇到了一些麻烦。目前我有两个文本框来搜索数据库并显示结果(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>
4

1 回答 1

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 + '%'"
}

PS请使用您各自语言的语法。

于 2013-05-31T08:55:30.983 回答