无法从ASP.NET中的SQL数据库获得准确的值

时间:2016-04-21 06:25:10

标签: c# asp.net

我正在创建ASP.NET,C#应用程序。输入值结果时,两个文本框的位置在SQL Server的gridview中显示。问题是,当我输入数字5时,我得到15个值,example。有人可以提示吗?

如何从文本框中输入的数据库中获取准确的值?

源代码:

SQL过程

Create table Hall
(
    HallID int,
    Name nvarchar(50),
    TicketLimit int
)
GO

Create table HallGroup
(
    HallID int,
    HallGroupID int,
    Name nvarchar(50),
    AZ int
)
GO

Create table HallSeat
(
    HallGroupID int,
    ShowSeatID int,
    Color nvarchar(15),
    Price int,
    SeatRow int,    
    SeatNumber int, 
    IsReserved bit
)

/**Search Procedure ****/

USE [Reservations]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Search]
@searchNumber varchar(50),
@searchRow varchar(50)

as
Begin   
    select * from HallSeat where (SeatNumber like '%' + @searchNumber + '%') and (SeatRow  like '%' + @searchRow + '%')

End

代码背后

protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;

        SqlConnection result = new SqlConnection(cs);

        String search = "Search"; // Paieskos         
        SqlCommand find = new SqlCommand(search, result);
        find.CommandType = CommandType.StoredProcedure;
        find.Parameters.Add("@searchRow", SqlDbType.VarChar).Value = TextBoxRow.Text;
        find.Parameters.Add("@searchNumber", SqlDbType.VarChar).Value = TextBoxNumber.Text;

        result.Open();
        SqlDataReader dr;
        dr = find.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Read();

            rep_bind();
            GridView1.Visible = true;

            TextBoxRow.Text = "";
            TextBoxNumber.Text = "";
        }
        else
        {
            GridView1.Visible = false;

        }

    }

.aspx的

<div class="container">      

        <asp:TextBox ID="TextBoxRow" placeholder="Type Seat Row" runat="server" style="border-radius: 5px;" Height="35px"></asp:TextBox>             
        <asp:TextBox ID="TextBoxNumber" placeholder="Type Seat Number" runat="server" style="border-radius: 5px;" Height="35px"></asp:TextBox>
        <br>
        <br>
        <br>

        <asp:Button ID="ButtonSearch" runat="server" CssClass="button" Text="Search for availability" OnClick="ButtonSearch_Click" OnClientClick="true" UseSubmitBehavior="false"  />
        <style type="text/css">
            .button {
              background:white;
              color: black;
              border: solid 3px #6496c8;
              font-family: cursive;
              font-size: 18px;
            }
            .button:hover,
            .button.hover
            {
              border-color: #346392;
              background: #7ABCDC;
              color: white;            
            }
            .button:active{
              border-color: #27496d;
              color: #27496d;
            }

        </style>
    </div>

    <!-- Paieskos rezultatu isvedimas GridView-->

 <div id="GRID">
   <asp:GridView ID="GridView1" runat="server"  HorizontalAlign="Center" AllowPaging="True" CellPadding="3" Width="568px"  GridLines="Vertical" Visible="False" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">       

        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#121315" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />  

           <AlternatingRowStyle BackColor="#DCDCDC" />

      <Columns>          

         <asp:TemplateField HeaderText="HALL">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HallGroupID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("HallGroupID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


          <asp:TemplateField HeaderText="ID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="COLOR">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="PRICE">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>  

          <asp:TemplateField HeaderText="ROW">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField> 

          <asp:TemplateField HeaderText="NUMBER">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>               

           <asp:TemplateField HeaderText="STATUS" SortExpression="Active">
                    <ItemTemplate><%# (Boolean.Parse(Eval("IsReserved").ToString())) ? "RESERVED" : "FREE" %></ItemTemplate>
             </asp:TemplateField>            

            <asp:TemplateField>                  
                <ItemTemplate>                        
                   <asp:Button ID="ButtonReserve" style="background:#2ACC16; border-radius:8px;" runat="server" CommandName="Reserve" HeaderText="BOOK SEAT" Text="Reserve" OnClick="ButtonReserve_Click" />                
                </ItemTemplate>            
             </asp:TemplateField> 

       </Columns>                      
   </asp:GridView>

3 个答案:

答案 0 :(得分:5)

您在SQL端使用like运算符。它不会比较,因为它会搜索一个模式。在您的示例中,它将使所有记录中都包含5

有关详细信息,请访问Like operator

要解决您的解决方案,请使用比较确切输入的=运算符。

select * from HallSeat where (SeatNumber = +'+ @searchNumber +'+ ) and (SeatRow  like '%' + @searchRow + '%')

答案 1 :(得分:1)

由于您使用like '%5%'进行搜索,请尝试使用= '5'

进行搜索

答案 2 :(得分:0)

您必须将datareader绑定到gridview,就像在此示例中一样 在执行绑定之前,不要执行datarader.Read()。

 using (SqlConnection con = new SqlConnection(strConnString))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "select top 10 * from Customers";
        cmd.Connection = con;
        con.Open();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();
    }
}

正如其他人所说,您的存储过程不正常。 应该是这样的

ALTER Procedure [dbo].[Search]
@searchNumber int,
@searchRow int

as
Begin   
    select * from HallSeat where SeatNumber = @searchNumber and SeatRow = @searchRow

End