如果用户已经存在,Windows窗体如何检入数据库

本文关键字:窗体 何检入 数据库 Windows 用户 存在 如果 | 更新日期: 2023-09-27 18:08:50

我想创建一个简单的表单,如果存在相同的用户,我想验证以显示消息。

函数usernamecheck()检查验证并显示错误,如果相同的用户存在,但当我点击提交按钮时,它仍然提交相同的用户。

 private void submit_Click(object sender, EventArgs e)
 {
  SqlConnection con = new SqlConnection();
  con.ConnectionString = "Data Source=LFC;Initial Catalog=contactmgmt;Integrated Security=True";
  con.Open();
  SqlCommand cmd = new SqlCommand();
  cmd.CommandText = "INSERT INTO cntc_employee (emp_f_name,emp_l_name,emp_alias,emp_contact_no,emp_address,emp_company,emp_bdate) VALUES(@fname,@lname,@alias,@contact,@address,@company,@bdate)";
  cmd.Connection = con;
  cmd.Parameters.AddWithValue("@fname", textBox1.Text);
  cmd.Parameters.AddWithValue("@lname", textBox2.Text);
  cmd.Parameters.AddWithValue("@alias", textBox3.Text);
  cmd.Parameters.AddWithValue("@contact", textBox4.Text);
  cmd.Parameters.AddWithValue("@address", textBox5.Text);
  cmd.Parameters.AddWithValue("@company", textBox6.Text);
  cmd.Parameters.AddWithValue("@bdate", textBox7.Text.ToString());
  UserNameCheck();
  cmd.ExecuteNonQuery();
  con.Close();
  MessageBox.Show("Data Inserted Succesfully");
}
public void UserNameCheck()
{
  string constring = "Data Source=LFC;Initial Catalog=contactmgmt;Integrated Security=True";
  SqlConnection con = new SqlConnection(constring);
  SqlCommand cmd = new SqlCommand("Select * from cntc_employee where emp_alias= @alias", con);
  cmd.Parameters.AddWithValue("@alias", this.textBox3.Text);
  con.Open();
  SqlDataReader dr = cmd.ExecuteReader();
  while (dr.Read())
  {
    if (dr.HasRows == true)
    {
       MessageBox.Show("Alias "+ dr[1].ToString() +" Already exist");
       break;
    }
  }
}

如果用户已经存在,Windows窗体如何检入数据库

问题:插入记录时总是没有检查用户是否存在

解决方案:您需要返回UserNameCheck()函数的布尔值。

如果username存在,

返回true。如果username不存在,返回false

则执行Insert Query当且仅当UserNameCheck函数返回false

试试这个:

修改UserNameCheck()函数代码如下,返回boolean的值。

public bool UserNameCheck()
{        
    string constring = "Data Source=LFC;Initial Catalog=contactmgmt;Integrated Security=True";
    SqlConnection con = new SqlConnection(constring);
    SqlCommand cmd = new SqlCommand("Select count(*) from cntc_employee where emp_alias= @alias", con);
    cmd.Parameters.AddWithValue("@alias", this.textBox3.Text);
    con.Open();
    int TotalRows = 0;
    TotalRows = Convert.ToInt32(cmd.ExecuteScalar());
    if(TotalRows > 0)
    {            
           MessageBox.Show("Alias "+ dr[1].ToString() +" Already exist");
           return true;
    }
    else
    {
           return false;
    }
}

现在更改Submit函数代码以验证UserNameCheck()返回值,只有当UserNameCheck()函数返回false(当user不存在时)才能进行插入

private void submit_Click(object sender, EventArgs e)
{
   if(!UserNameCheck())
   {
    SqlConnection con = new SqlConnection();
    con.ConnectionString = "Data Source=LFC;Initial Catalog=contactmgmt;Integrated Security=True";
    con.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "INSERT INTO cntc_employee (emp_f_name,emp_l_name,emp_alias,emp_contact_no,emp_address,emp_company,emp_bdate) VALUES(@fname,@lname,@alias,@contact,@address,@company,@bdate)";
    cmd.Connection = con;
    cmd.Parameters.AddWithValue("@fname", textBox1.Text);
    cmd.Parameters.AddWithValue("@lname", textBox2.Text);
    cmd.Parameters.AddWithValue("@alias", textBox3.Text);
    cmd.Parameters.AddWithValue("@contact", textBox4.Text);
    cmd.Parameters.AddWithValue("@address", textBox5.Text);
    cmd.Parameters.AddWithValue("@company", textBox6.Text);
    cmd.Parameters.AddWithValue("@bdate", textBox7.Text.ToString());

    cmd.ExecuteNonQuery();
    con.Close();
    MessageBox.Show("Data Inserted Succesfully");
 }
}

首先执行一个select查询,检查用户是否存在。

您应该首先为用户制作select count by name,如果counter大于0,则制作insert