这是MasterPage.master我的搜索按钮'='附近的语法不正确错误:dr = cmd.ExecuteReader();
<asp:TextBox ID="Search" style="width:339px ; height: 20px;" runat="server" placeholder="Search Woooo" class="textbox" formnovalidate="formnovalidate"></asp:TextBox>
<asp:ImageButton ID="ImageButtonSearch" runat="server" ImageUrl="~/Images/Icons/search.png" Width="22px" OnClick="ImageButtonSearch_Click" CausesValidation="False" />
MasterPage.master.cs
protected void ImageButtonSearch_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("~/search.aspx?Search=" + Search.Value);
}
search.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class Default2 : System.Web.UI.Page
{
string cs = Global.CS;
protected void Page_Load(object sender, EventArgs e)
{
string search = Request.QueryString["search"];
if (!Page.IsPostBack)
{
string xx = "";
string sql = @"SELECT studentID FROM Student WHERE username == '"+ search + "'";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand(sql, con);
con.Open();
SqlDataReader dr;
dr = cmd.ExecuteReader();
while (dr.Read())
{
xx += string.Format("{0}",dr["sudentID"]);
}
dr.Close();
con.Close();
test.InnerHtml = xx;
}
}
}
我想尝试基于搜索的用户名上搜索框。但是,当我点击搜索按钮后,它会在'='附近显示不正确的语法。源错误:dr = cmd.ExecuteReader();.我已经在很多地方搜索了这个问题,但我仍然无法理解。我还是新的C#
解决!谢谢 –
我不希望他们进入'';删除学生表 - - 作为用户名。 –
@PatrickHofman当然,你注意到了,我只指出了例外的原因。感谢downvoting :) – zaitsman