在注册表单中插入查询

时间:2015-08-27 20:15:30

标签: c# sql insert registration

protected void Page_Load(object sender, EventArgs e)
  {

  }
protected void Page_Load(object sender, EventArgs e)
  {

  }
protected void registerBtn_Click1(object sender, EventArgs e)
  {
    Page.Validate();
    if (Page.IsValid)
      {
       outputlabel.Text = "Succesfully Registered";
       myConnection.Open();
       string query = "Insert into Users (FirstName,LastName,Gender,DateOfBirth,
         Email_Id,Password) Values (@fn,@ln,@gen,@dob,@em,@pas)";
       SqlCommand insertCommand = new SqlCommand(query, myConnection);
       insertCommand.Parameters.AddWithValue("@fn", fname.Text);
       insertCommand.Parameters.AddWithValue("@ln", lname.Text);
       insertCommand.Parameters.AddWithValue("@gen", gender_dd.SelectedItem.Text);
       insertCommand.Parameters.AddWithValue("@dob", date.Text);
       insertCommand.Parameters.AddWithValue("@em", email.Text);
       insertCommand.Parameters.AddWithValue("@pas", password.Text);
       insertCommand.ExecuteNonQuery();
       myConnection.Close();
      }
    }
  }
}

帮我这个代码..我可以编辑这个。如果输入相同的信息。代码显示消息已经注册。所以我的数据库没有重复。

1 个答案:

答案 0 :(得分:0)

您需要运行一条SQL语句,检查用户当前是否存在 执行INSERT into USERS语句之前的数据库

因此,下面的代码将根据用户提供的电子邮件地址密码值检查用户是否已存在。

string userInvalid = "The username entered is invalid, please choose another."
string checkDatabase = "SELECT * FROM Users WHERE Email_Id = @em AND Password = @pas";
SqlCommand command = new SqlCommand(checkDatabase, myConnection);
command.Parameters.AddWithValue("@em", email.Text);
command.Parameters.AddWithValue("@pas", password.Text);
command.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();

if (reader.HasRows) {
  outputlabel.Text = userInvalid;
}
else 
{
  // execute your original SQL query 
}