简体   繁体   English

如何使用文本框在ASP.NET中搜索数据库

[英]How to search a database in ASP.NET using a text box

I got a quick question, I am building an ASP.NET website and have a database set up (SQL Server 2008). 我有一个快速的问题,我正在建立一个ASP.NET网站并建立了一个数据库(SQL Server 2008)。 One one of my pages I am displaying all of all of the entries of the database with a search text box at the top. 我的一页中,我将显示数据库的所有条目,并在顶部显示一个搜索文本框。 How would I go about taking the value that the user entered and using that string to display only the data entries that apply to it. 我将如何处理用户输入的值,并使用该字符串仅显示适用于它的数据条目。

Here is my code for the .aspx file 这是我的.aspx文件代码

<%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Layout.Master" CodeBehind="db.aspx.vb" Inherits="Project4.db" %>

    <fieldset>
        <legend>Search</legend>
        Last Name: <asp:TextBox ID="LastNameSearch" runat="server"></asp:TextBox>
        <asp:Button ID="Submit" runat="server" Text="Search" /> <br />
    </fieldset>



<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" 
    AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" 
    BorderStyle="None" BorderWidth="1px" CellPadding="4" 
    DataKeyNames="FirstName,LastName,PhoneNum" AllowPaging="True">
    <Columns>
        <asp:CommandField ShowEditButton="True" />
        <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
            SortExpression="FirstName" ReadOnly="True" />
        <asp:BoundField DataField="LastName" HeaderText="LastName"
            SortExpression="LastName" ReadOnly="True" />
        <asp:BoundField DataField="PhoneNum" HeaderText="PhoneNum"
            SortExpression="PhoneNum" ReadOnly="True" />
        <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        <asp:CommandField ShowDeleteButton="True" />
    </Columns>
    <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
    <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
    <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
    <RowStyle BackColor="White" ForeColor="#003399" />
    <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
    <SortedAscendingCellStyle BackColor="#EDF6F6" />
    <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
    <SortedDescendingCellStyle BackColor="#D6DFDF" />
    <SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" 
    SelectCommand="SELECT [FirstName], [LastName], [PhoneNum], [Email] FROM [MailList]" 
    ViewStateMode="Disabled" OldValuesParameterFormatString="original_{0}">

</asp:SqlDataSource>

Now I want to search the string and compare it to the second column of the database the LastName and only display the records where the last name matches that. 现在,我想搜索字符串并将其与数据库的第二列LastName进行比较,并仅显示姓氏与之匹配的记录。 Any help would be greatly appreciated 任何帮助将不胜感激

This should be pretty close, doing it from memory though so it may be off a little. 这应该是非常接近的,尽管是从内存中完成的,所以可能有点偏离。 It should be enough to get you going in the right direction though I think 虽然我认为这足以使您朝正确的方向前进

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:Database1ConnectionString %>" 
    SelectCommand="SELECT [FirstName], [LastName], [PhoneNum], [Email] FROM [MailList] WHERE [LastName] = @lastName" 
    ViewStateMode="Disabled" OldValuesParameterFormatString="original_{0}">
    <SelectParameters>
        <asp:ControlParameter ControlID="LastNameSearch" Name="lastName" defaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>

you have to configure your datatabase to performs contains and freetext querys, that way you can perform a search onto your database base on the data that match your fields criteria. 您必须将数据库配置为执行包含和自由文本查询,这样您就可以根据与字段条件匹配的数据对数据库执行搜索。

how to search a database 如何搜索数据库

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

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