C# 我正在开发一个新的用户注册表单,它只包含三个字段:用户名、密码和确认密码

C# 我正在开发一个新的用户注册表单,它只包含三个字段:用户名、密码和确认密码,c#,authentication,parameter-passing,sql-insert,password-confirmation,C#,Authentication,Parameter Passing,Sql Insert,Password Confirmation,我正在编写一份用户注册表,其中只包含用户名、密码和确认密码三个字段。但当我插入数据时,如果密码不匹配,则会出现表单不匹配异常,但单击“确定”后,数据将插入数据库。 我应该怎么做,它只插入匹配的密码 private void btn_save_Click(object sender, EventArgs e) { try { conn.Open(); OleDbCommand command = new OleDbCommand();

我正在编写一份用户注册表,其中只包含用户名、密码和确认密码三个字段。但当我插入数据时,如果密码不匹配,则会出现表单不匹配异常,但单击“确定”后,数据将插入数据库。 我应该怎么做,它只插入匹配的密码

private void btn_save_Click(object sender, EventArgs e)
{
    try
    {
         conn.Open();
         OleDbCommand command = new OleDbCommand();
         command.Connection = conn;
         string query = "INSERT INTO Users (username,newpassword)values('" + txt_newusr.Text + "','" + txt_password.Text + "')";
         if (txt_password.Text == "" || txt_cnfpw.Text == "")
         {
             MessageBox.Show("Please enter values");
             return;
         }
         if (txt_password.Text != txt_cnfpw.Text)
         {
             MessageBox.Show("Password confirm password are not matching");
             txt_cnfpw.Focus();
         }
         MessageBox.Show(query);
         command.CommandText = query;
         command.ExecuteNonQuery();
         MessageBox.Show("Record Saved successfully");
         conn.Close();
   }
}

你应该这样改变它

if (txt_password.Text == txt_cnfpw.Text)
{
    MessageBox.Show(query);
    command.CommandText = query;
    command.ExecuteNonQuery();
    MessageBox.Show("Record Saved successfully");
}

你应该这样改变它

if (txt_password.Text == txt_cnfpw.Text)
{
    MessageBox.Show(query);
    command.CommandText = query;
    command.ExecuteNonQuery();
    MessageBox.Show("Record Saved successfully");
}

在成功和失败的情况下,您都试图提交事务。 仅当密码匹配时才应执行Save语句。将save语句移动到success块中,如下所示

if (txt_password.Text == txt_cnfpw.Text)
{
    MessageBox.Show(query);
    command.CommandText = query;
    command.ExecuteNonQuery();
    MessageBox.Show("Record Saved successfully");
}  
else
{
    MessageBox.Show("Password confirm password are not matching");
    txt_cnfpw.Focus();
}

在成功和失败的情况下,您都试图提交事务。 仅当密码匹配时才应执行Save语句。将save语句移动到success块中,如下所示

if (txt_password.Text == txt_cnfpw.Text)
{
    MessageBox.Show(query);
    command.CommandText = query;
    command.ExecuteNonQuery();
    MessageBox.Show("Record Saved successfully");
}  
else
{
    MessageBox.Show("Password confirm password are not matching");
    txt_cnfpw.Focus();
}

您必须进行大量更正才能使其正常工作,更正如下:

  • 使用参数化查询代替串联查询以避免注入
  • 仅在客户端验证后处理插入(空检查密码匹配等)
  • 使用管理连接和命令
我在下面添加了一个示例,请看一看

try
{
    string query = "INSERT INTO Users (username,newpassword)values(@username,@newpassword)";
    bool CanInsertNewUser = true;
    if (txt_newusr.Text=="" || txt_password.Text == "" || txt_cnfpw.Text == "")
    {
        CanInsertNewUser = false;
        MessageBox.Show("Please enter values");
    }
    if (txt_password.Text != txt_cnfpw.Text)
    {
        CanInsertNewUser = false;
        MessageBox.Show("Password confirm password are not matching");
        txt_cnfpw.Focus();
    }
    if (CanInsertNewUser)
    {
        using (OleDbConnection conn = new OleDbConnection("GiveYourConnectionStringHere"))
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                conn.Open();
                command.Connection = conn;
                command.CommandText = query;
                command.Parameters.Add("@username", OleDbType.VarChar).Value = txt_newusr.Text;
                command.Parameters.Add("@newpassword", OleDbType.VarChar).Value = txt_password.Text;
                command.ExecuteNonQuery();
            }
        }
        MessageBox.Show("Success");
    }

}
catch (Exception ex)
{
    MessageBox.Show("OLEDB issues : " + ex.Message.ToString());
}

您必须进行大量更正才能使其正常工作,更正如下:

  • 使用参数化查询代替串联查询以避免注入
  • 仅在客户端验证后处理插入(空检查密码匹配等)
  • 使用管理连接和命令
我在下面添加了一个示例,请看一看

try
{
    string query = "INSERT INTO Users (username,newpassword)values(@username,@newpassword)";
    bool CanInsertNewUser = true;
    if (txt_newusr.Text=="" || txt_password.Text == "" || txt_cnfpw.Text == "")
    {
        CanInsertNewUser = false;
        MessageBox.Show("Please enter values");
    }
    if (txt_password.Text != txt_cnfpw.Text)
    {
        CanInsertNewUser = false;
        MessageBox.Show("Password confirm password are not matching");
        txt_cnfpw.Focus();
    }
    if (CanInsertNewUser)
    {
        using (OleDbConnection conn = new OleDbConnection("GiveYourConnectionStringHere"))
        {
            using (OleDbCommand command = new OleDbCommand())
            {
                conn.Open();
                command.Connection = conn;
                command.CommandText = query;
                command.Parameters.Add("@username", OleDbType.VarChar).Value = txt_newusr.Text;
                command.Parameters.Add("@newpassword", OleDbType.VarChar).Value = txt_password.Text;
                command.ExecuteNonQuery();
            }
        }
        MessageBox.Show("Success");
    }

}
catch (Exception ex)
{
    MessageBox.Show("OLEDB issues : " + ex.Message.ToString());
}


放置
返回按钮
txt_cnfpw.Focus()之后并查看是什么happening@sujithkarivelil它捕获错误异常,即连接未关闭,焦点后缺少返回,因此执行查询。在对查询执行任何操作之前,您应该先验证数据,也就是说,在尝试之前先验证数据,这样做会更好地使用不同的方法。SQL注入警告--您的查询完全容易受到攻击。请使用@sujithkarivelil provided@OlivierRogier非常感谢您宝贵的意见,将
返回
txt_cnfpw.Focus()之后并查看是什么happening@sujithkarivelil它捕获错误异常,即连接未关闭,焦点后缺少返回,因此执行查询。在对查询执行任何操作之前,您应该先验证数据,也就是说,在尝试之前先验证数据,这样做会更好地使用不同的方法。SQL注入警告--您的查询完全容易受到攻击。请使用@sujithkarivelil provided@OlivierRogier非常感谢您宝贵的意见,如果
txt\u password.Text
txt\u cnfpw.Text
为空怎么办?
txt\u newusr.Text
是否有空值?>@sujit karivelil您必须自己返回该值,以避免在空值情况下执行values@sujithkarivelil我认为这是把拯救声明放在成功的障碍之外,就像苏坎尼亚告诉thanx所有帮助我的人@MuhammadAli:Lol。。。包括对
txt\u newusr.Text
的空检查,以避免用户为空name@Sukanya字体我知道。我试图帮助你改进你的答案,因为这是你在这个论坛上的第一个答案(我想是基于你的声誉)。感谢您的帮助,衷心欢迎如果
txt\u password.Text
txt\u cnfpw.Text
为空怎么办?
txt\u newusr.Text
是否有空值?>@sujit karivelil您必须自己返回该值,以避免在空值情况下执行values@sujithkarivelil我认为这是把拯救声明放在成功的障碍之外,就像苏坎尼亚告诉thanx所有帮助我的人@MuhammadAli:Lol。。。包括对
txt\u newusr.Text
的空检查,以避免用户为空name@Sukanya字体我知道。我试图帮助你改进你的答案,因为这是你在这个论坛上的第一个答案(我想是基于你的声誉)。感谢您的帮助和衷心的欢迎