使用存储过程在数据库中搜索

时间:2018-12-05 12:56:50

标签: c# asp.net visual-studio webforms visual-studio-2017

好,所以我试图在以下存储过程中为2个参数构建一个具有2个搜索字段的网站,并使用它们进行搜索,我遵循了发现的在线教程,该存储过程如下:

CREATE PROC Original_Content_Search
@typename VARCHAR(20),
@categoryname VARCHAR(20)
AS
IF @typename is NULL
SELECT *
FROM Original_Content OC INNER JOIN Content C ON  OC.id=C.id
WHERE OC.filter_status=1 and OC.review_status=1 and 
C.category_type=@categoryname
ELSE
SELECT OC.*
FROM Original_Content OC INNER JOIN Content C ON  OC.id=C.id
WHERE OC.filter_status=1 and OC.review_status=1 and C.[type]=@typename

该过程正常运行,因为我尝试通过查询运行该过程,并且运行良好,现在,我正在尝试根据我遵循的教程使用以下内容从搜索中构建网格,但是在刷新页面时我点击搜索

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<div>
Search Original Content:
<br />
Search By Type: <asp:TextBox ID="typeSearch" runat="server"></asp:TextBox>
<br />
Search By Category: <asp:TextBox ID="categorySearch" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnSearch" runat="server" Text="Search" />
<hr />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    runat="server" AutoGenerateColumns="false" DataSourceID="GridDataSource" AllowPaging="true">
    <Columns>
        <asp:BoundField DataField="id" HeaderText="ID" ItemStyle-Width="150" />
        <asp:BoundField DataField="content_manager_id" HeaderText="Content Manager ID" ItemStyle-Width="150" />
        <asp:BoundField DataField="reviewer_id" HeaderText="Reviewer ID" ItemStyle-Width="150" />
        <asp:BoundField DataField="review_status" HeaderText="Review Status" ItemStyle-Width="150" />
        <asp:BoundField DataField="filter_status" HeaderText="Filter Status" ItemStyle-Width="150" />
        <asp:BoundField DataField="rating" HeaderText="Rating" ItemStyle-Width="150" />

    </Columns>
</asp:GridView>
<asp:SqlDataSource ID="GridDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ConStr %>"
    SelectCommand="Original_Content_Search" SelectCommandType="StoredProcedure">
    <SelectParameters>
        <asp:ControlParameter Name="typename" ControlID="typeSearch" PropertyName="Text" DefaultValue="" ConvertEmptyStringToNull="true" />
        <asp:ControlParameter Name="categoryname" ControlID="categorySearch" PropertyName="Text" DefaultValue="" ConvertEmptyStringToNull="true" />
    </SelectParameters>
</asp:SqlDataSource>
    </div>
</asp:Content>

正如我所说,这只会刷新页面,我假设我需要以某种方式将按钮与搜索链接起来?或者我应该如何使它工作?非常感谢

0 个答案:

没有答案