简体   繁体   English

如何在1个DataSource上添加多个SQL语句/查询字符串?

[英]How to add more than one SQL statement/Query String on 1 DataSource?

I currently have the following statement with QueryString set to ProductCategory on my ListView control: 我目前有以下语句,QueryString设置为ListView控件上的ProductCategory:

SELECT * FROM Products WHERE (ProductCategory = ?)

I would like to add this statement and query string on the same DataSource to allow query string for ProductModel as well, how can I do that? 我想在同一个DataSource上添加此语句和查询字符串以允许ProductModel的查询字符串,我该怎么做?

SELECT * FROM Products WHERE (ProductModel = ?)

ListVIew 列表显示

  <asp:ListView runat="server" ID="listView" GroupItemCount="3" 
        DataSourceID="AccessDataSource1">
    <LayoutTemplate>
        <div style="height: 966px;">
        <div style="width: 771px;">
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
        <asp:DataPager runat="server" ID="dpMyDatePager" PageSize="9"
 PagedControlID="listView">
<Fields>
    <asp:NextPreviousPagerField ButtonType="Button" ShowLastPageButton="True"
     ShowFirstPageButton="True" />
     <asp:NumericPagerField />
</Fields>
</asp:DataPager>
        </div>
    </LayoutTemplate>
    <GroupTemplate>
        <div style="clear: both;">
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
        </div>
    </GroupTemplate>
    <ItemTemplate>
<div class="productItem">
    <div>
        <img src='<%# Eval("ProductUrl") %>' >
    </div>
    <div class="catalog-price">
    <br />    
            <%# Eval("ProductBrand") %>
            <%# Eval("ProductModel") %></div>

    <div class="catalog-price2">
        <b>
        Our Price: S$<%# Eval("NormalPrice") %></div>
        </b><br />
    <div class="cell1">
        Add to cart</div>
    <div class="cell2">
    <asp:HyperLink ID="HypViewDetails" style="text-decoration:none"      NavigateUrl='<%#Eval("ProductId", "ProductDetails.aspx?ProductID={0}")%>'
 runat="server">View Details</asp:HyperLink>
        </div>
</div>
</ItemTemplate>
    <ItemSeparatorTemplate>
        <div class="itemSeparator">
        </div>
    </ItemSeparatorTemplate>
    <GroupSeparatorTemplate>
        <div class="groupSeparator">
        </div>
    </GroupSeparatorTemplate>
    <EmptyDataTemplate>
    </EmptyDataTemplate>
</asp:ListView>
    <asp:AccessDataSource ID="AccessDataSource1" runat="server" 
        DataFile="~/App_Data/TabStoreDB.mdb" 

            SelectCommand="SELECT * FROM Products WHERE (ProductCategory = ?) OR    (ProductBrand = ?)">
        <SelectParameters>
            <asp:QueryStringParameter Name="?" QueryStringField="ProductCategory" />
            <asp:QueryStringParameter Name="?" QueryStringField="ProductBrand" />
        </SelectParameters>
    </asp:AccessDataSource>

Unless I am totally not understanding your problem. 除非我完全不了解你的问题。 You can just add AND/OR to you WHERE clause 您只需向WHERE子句添加AND/OR即可

OR WHERE clause OR WHERE子句

SELECT * 
FROM Products 
WHERE (ProductCategory = ?)
OR (ProductModel = ?)

AND WHERE Clause AND WHERE子句

SELECT * 
FROM Products 
WHERE (ProductCategory = ?)
AND (ProductModel = ?)

Based on your edit you have both query parameters are ? 根据您的编辑,您有两个查询参数? - they each need to have a specific name - 他们每个人都需要有一个特定的名字

SelectCommand="SELECT * FROM Products WHERE (ProductCategory = @ProductCategory) OR    (ProductBrand = @ProductBrand)">
        <SelectParameters>
            <asp:QueryStringParameter Name="ProductCategory" QueryStringField="ProductCategory" />
            <asp:QueryStringParameter Name="ProductBrand" QueryStringField="ProductBrand" />
        </SelectParameters>

If you are unsure of how to use the parameters, I suggest the following reading: 如果您不确定如何使用这些参数,我建议您阅读以下内容:

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters.aspx http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters.aspx

http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx http://msdn.microsoft.com/en-us/library/xt50s8kz.aspx

You gave both parameters the same name. 您为这两个参数指定了相同的名称。 You need to be specfic -- try this: 你需要特定 - 试试这个:

SelectCommand="SELECT * FROM Products WHERE (ProductCategory = @cat) OR    (ProductBrand = @brand)">

And then this: 然后这个:

<SelectParameters>
            <asp:QueryStringParameter Name="cat" QueryStringField="ProductCategory" />
            <asp:QueryStringParameter Name="brand" QueryStringField="ProductBrand" />
        </SelectParameters>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM