Sql 筛选出重复用户Asp.Net

Sql 筛选出重复用户Asp.Net,sql,asp.net,sql-server,webforms,ado.net,Sql,Asp.net,Sql Server,Webforms,Ado.net,因此,我在后端创建了一个注册页和一个SQL tableStudents表。在my registration page.aspx的代码隐藏中,我有一个sql查询来计算数据库中StudentName的数量,如果该数量等于1,则通知试图注册的用户数据库中已经存在该学生。然而,每次我测试它时,计数总是为0,即使我使用已经在db中的学生名注册 protected void Page_Load(object sender, EventArgs e) { if (IsPostB

因此,我在后端创建了一个注册页和一个SQL tableStudents表。在my registration page.aspx的代码隐藏中,我有一个sql查询来计算数据库中StudentName的数量,如果该数量等于1,则通知试图注册的用户数据库中已经存在该学生。然而,每次我测试它时,计数总是为0,即使我使用已经在db中的学生名注册

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["RegistrationConnectionString"].ConnectionString);
            conn.Open();
            string checkUser = "select count(*) from Students where StudentName='" + txtStudentName.Text + "'";
            SqlCommand com = new SqlCommand(checkUser, conn);
            int temp = Convert.ToInt32(com.ExecuteScalar().ToString());
            if (temp == 1)
            {
                Response.Write("Student already exist");
            }


            conn.Close();
        }

    }

警告:您的代码极易受到sql注入攻击!您使用的是MySql还是Sql Server?因为问题被标记为MySql,但您使用的是Sql Server ADO.Net提供程序。MySql和Sql Server是两种截然不同的关系数据库引擎。