我在 asp.net 中不断收到运行时 SQL 查询错误。我正在使用 c#。错误总是以“(某些单词)”附近的不正确语法开头。我已经检查并重新检查了我的代码是否有任何语法错误,但从未发现任何错误。在下面的代码中,错误是“用户”附近的语法不正确。请帮忙。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class LogIn : System.Web.UI.Page
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
con.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users \Sony\Documents\Library\App_Data\Library.mdf;Integrated Security=True;User Instance=True";
cmd.Connection=con;
con.Open();
}
protected void txt_user_TextChanged(object sender, EventArgs e)
{
}
protected void txt_pass_TextChanged(object sender, EventArgs e)
{
}
protected void btn_log_Click(object sender, EventArgs e)
{
cmd.CommandText="select count(*) from user where Username='"+txt_user.Text+"' and Password='"+txt_pass.Text+"'";
int count =Convert.ToInt16(cmd.ExecuteScalar());
if (count==1)
{
Response.Redirect("Home.aspx");
}
else
{
Label1.Text="Invalid Username or Password. Please try again..";
}
}