gpt4 book ai didi

c# - 无法从 ASP.NET 中的 SQL 数据库中获取准确的值

转载 作者:行者123 更新时间:2023-11-30 20:20:27 32 4
gpt4 key购买 nike

我正在创建 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>

最佳答案

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

欲了解更多信息,请访问 Like operator

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

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

关于c# - 无法从 ASP.NET 中的 SQL 数据库中获取准确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36761267/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com