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

时间:2011-08-01 17:59:25

标签: asp.net sql database visual-studio ms-access

我目前在ListView控件上将QueryString设置为ProductCategory时声明如下:

SELECT * FROM Products WHERE (ProductCategory = ?)

我想在同一个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>

2 个答案:

答案 0 :(得分:2)

除非我完全不了解你的问题。您只需将AND/OR添加到WHERE子句

即可

OR WHERE条款

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

AND WHERE条款

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

根据您的编辑,您有两个查询参数? - 他们每个人都需要有一个特定的名字

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

如果您不确定如何使用这些参数,我建议您阅读以下内容:

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

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

答案 1 :(得分:0)

您有两个参数同名。你需要具体 - 试试这个:

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

然后这个:

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