2013-05-30 45 views
0

我在獲取所需輸出時遇到了一些麻煩。目前我有兩個文本框搜索數據庫並顯示結果(SerialNumber和SiteID)。現在只有當這兩個框都填滿時,才顯示數據。但是,我想要的是,如果序列號是空白的,並且站點ID是3,則會播放站點ID爲3的所有數據。如果序列號爲T7且站點ID爲空白,則顯示序列號爲T7的所有數據。如果兩個字段均爲空,則不顯示任何內容最後,如果SerialNum是T7並且SiteID是3,則顯示所有T7和3的數據。Select語句中的案例條款Visual Web Developer

當前:當其中一個框爲空時,數據庫中不顯示任何內容。

這是我在使用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> 

回答

0

在這裏,你必須保持在在textbox- 參數的檢查首先刪除其中來自主查詢條件,該附加哪裏取決於參數條件中提到

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請使用各自語言的語法。

+0

我實際上犯了一個錯誤Nirmesh。我沒有使用C#,我正在使用Basic。你能否對你的答案做出必要的修改? –

+0

你的問題是在SQL查詢。不是其他基本代碼。這只是一個僞代碼..你可以用你選擇的任何語言來嘗試。只要檢查 - 如果兩個參數都存在,則在where子句中使用「和」。如果只有一個參數存在,那麼在where子句中只使用該參數。 –