简体   繁体   English

ASP SQL如何检查数据库表中是否已经存在用户名?

[英]ASP SQL How to check if username already exists in database table?

I am currently working with SQL database and my assignment is to make a registration form. 我目前正在使用SQL数据库,我的任务是填写注册表。 I have got the registration form to work but I need to check if username have already been taken. 我有注册表格,但是我需要检查用户名是否已被使用。 In my code Username is in the form of Emails. 在我的代码中,用户名采用电子邮件的形式。 The code I have works, but as it is, multiple usernames are allowed. 我拥有的代码有效,但实际上,允许使用多个用户名。 HEre is my code: 这是我的代码:

protected void registerUser(Object src, EventArgs e)
    {
        Response.Write("you have connected to your .cs page add records");
        get_connection();
        try
        {
            connection.Open();
            command = new SqlCommand("INSERT INTO subscribers (FirstName, LastName, Email, Password)" +
                " VALUES (@FirstName, @LastName, @Email, @Password)", connection);

            command.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
            command.Parameters.AddWithValue("@LastName", txtLastName.Text);
            command.Parameters.AddWithValue("@Email", txtEmail.Text);
            command.Parameters.AddWithValue("@Password", txtPassword.Text);

            command.ExecuteNonQuery();

            //connection.Close();
        }
        catch(Exception err)
        {
            lblInfo.Text = "Error reading the database. ";
            lblInfo.Text += err.Message;
        }
        finally
        {
            connection.Close();
            lblInfo.Text += "<br /><b>Record has been added</b>";
            //lblInfo.Text = "<b>Server Version:</b> " + connection.ServerVersion;
            lblInfo.Text += "<br /><b>Connection Is:</b> " + connection.State.ToString();
        }
    }

To check if the username had already been taken, I was thinking about using an "If Then" statement within the "try" area but am unsure what coding I would need. 为了检查用户名是否已经被占用,我正在考虑在“ try”区域中使用“ If Then”语句,但是不确定我需要什么编码。 Any help or advice would be appreciated. 任何帮助或建议,将不胜感激。

You can write something like this: 您可以这样写:

 string cmdText = @"IF NOT EXISTS(SELECT 1 FROM subscribers where Email = @Email) 
     INSERT INTO subscribers (FirstName, LastName, Email, Password)
                      VALUES (@FirstName, @LastName, @Email, @Password)"

 command = new SqlCommand(cmdText, connection);
 ......

You can try this code : 您可以尝试以下代码:

 string sqlQuery =   "IF NOT EXISTS (SELECT 1 FROM subscribers where Email = @Email) 
    BEGIN 
    INSERT INTO subscribers (FirstName, LastName, Email, Password) VALUES (@FirstName, @LastName, @Email, @Password)
    SELECT SCOPE_IDENTITY()
    END
    ELSE SELECT 0"

using (command = new SqlCommand())
{
    command.CommandText = sqlQuery;
    command.Parameters.AddWithValue("@FirstName", txtFirstName.Text);
    command.Parameters.AddWithValue("@LastName", txtLastName.Text);
    command.Parameters.AddWithValue("@Email", txtEmail.Text);
    command.Parameters.AddWithValue("@Password", txtPassword.Text);

    connection.Open();
    var res = (int)cmd.ExecuteScalar();
    connection.Close();
}

if a result is 0 then already exists otherwise new record inserted. 如果结果为0,则已经存在,否则插入新记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM