识别重复数据,并根据新用户输入重新搜索数据库

时间:2016-03-06 21:34:35

标签: c# mysql sql visual-studio

我已经能够确定该值是否重复,但仅针对输入的第一个值,似乎第一个条目被保留为搜索,并且它不会更新搜索值已在文本框中更改。有关视觉表示,请参阅下面的图片。

以下是我使用的方法:

private void CheckContactNumber()
    {

        DataSet myDataSet = new DataSet();

        try
        {
            string strAccessSelect = "select count(*) from Employee where ContactNumber='" + addContactNum.Text + "'";
            OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, conn);
            OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);

            conn.Open();
            myDataAdapter.Fill(myDataSet, "Employee");


        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
            return;
        }
        finally
        {
            conn.Close();
        }
        DataTable dt = myDataSet.Tables[0];
        if (dt != null)
        {
            if (int.Parse(dt.Rows[0][0].ToString()) > 0)
            {
                uniqueContactNumber = false;
            }
        }
    }

以下是checkContactNumber方法链接到的方法:

   private void addEmployee_Click(object sender, EventArgs e)
    {
        string err = "";

        if (addFirstName.Text.Trim() == "")
        {
            errorFirstName.Visible = true;
            err += "Enter a value for First Name\r\n";
        }
        else if (!Regex.IsMatch(addFirstName.Text, @"^[a-zA-Z]+$"))//has numerical and has a value
        {
            errorFirstName.Visible = true;
            err += "Enter a valid First Name\r\n";
        }

        else errorFirstName.Visible = false;

        if (addLastName.Text.Trim() == "")
        {
            errorLastName.Visible = true;
            err += "Enter a value for Last Name\r\n";
        }
        else if (!Regex.IsMatch(addLastName.Text, @"^[a-zA-Z]+$"))
        {
            errorLastName.Visible = true;
            err += "Enter a valid Last Name\r\n";
        }
        else errorLastName.Visible = false;


        if (addFirstName.Text.Trim() != "" && addLastName.Text.Trim() != "" && addFirstName.Text.Trim() == addLastName.Text.Trim())//identifies if FirstName + SecondName is equal to each other.
        {
            errorFirstName.Visible = true;
            errorLastName.Visible = true;
            err += "First Name and Second Name must be unique\r\n";
        }


        if (addRole.Text.Trim() == "")
        {
            errorRole.Visible = true;
            err += "Select a Role type\r\n";
        }
        else errorRole.Visible = false;

        if (!Regex.IsMatch(addContactNum.Text, @"^\d{11}$"))
        {
            errorContactNum.Visible = true;
            err += "Enter a value for Contact Number\r\n";
        }
        else errorContactNum.Visible = false;

        CheckContactNumber();

        if(uniqueContactNumber == false && addContactNum.Text != "")
        {
            err += "Contact Number Already exist..\r\n";
            errorContactNum.Visible = true;
        }
        if (err == "" && uniqueContactNumber == true)
        {
            string addEmployee = "INSERT INTO Employee (FirstName, LastName, Role, DateOfHire, ContactNumber)" +
                        "VALUES (@FirstName, @LastName, @Role, @DateOfHire, @ContactNumber)";

            OleDbCommand cmd = new OleDbCommand(addEmployee, conn);
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);
            cmd.Parameters.Add("@FirstName", OleDbType.VarChar).Value = addFirstName.Text;
            cmd.Parameters.Add("@LastName", OleDbType.VarChar).Value = addLastName.Text;
            cmd.Parameters.Add("@Role", OleDbType.VarChar).Value = addRole.Text;
            cmd.Parameters.Add("@DateOfHire", OleDbType.VarChar).Value = addDateOfHire.Text;
            cmd.Parameters.Add("@ContactNumber", OleDbType.VarChar).Value = addContactNum.Text;

            conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

            //addFirstName.Text = String.Empty;
            //addLastName.Text = String.Empty;
            //addRole.Text = String.Empty;
            //addContactNum.Text = String.Empty;
            addRole.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;



        }
        //Save It
        else
        {
            MessageBox.Show(err);
        }
    }

Here is an example image showing that it can identify if there is duplicate data.

This shows that I have re-entered to make it unique but it still says it exists.

This shows that I have closed the program and ran it again using the same contact number in image 2 and it then saves.

2 个答案:

答案 0 :(得分:1)

我建议您向Index添加UNIQUE约束,其中包含Employee表的ContactNumber字段,并且当您插入违反该约束的记录时,您会捕获MySQL抛出的错误

  1. 我看到的前两张图片看似相同(http://i.stack.imgur.com/a3WLr.pnghttp://i.stack.imgur.com/W2SoA.png

  2. 第3张图片(XOE0Q.png,抱歉,我不会发布其他链接)似乎与上述图片的编号不同。

  3. 因此,我认为你的代码做的是正确的,除非第二张图片是错误的。如果为图像2和图像1输入了不同的数字,那么在进一步输入时,您的表单可能不会再次调用validate方法。要验证这一点,请使用调试器并在checkContactNumber方法中设置断点,并验证是否再次调用它。此方法在contactNumber输入上绑定了哪个事件?

答案 1 :(得分:1)

如果在联系号码为重复时单击addEmployee按钮,则uniqueContactNumber设置为false,并显示一个消息框。如果您使用新的联系号码更改联系号码并再次单击该按钮,则CheckContactNumber方法不执行任何操作,只留下值为false的uniqueContactNumber。

在这种情况下,您需要确保将uniqueContactNumber设置为true。

您可以使用以下代码修复它:

if (int.Parse(dt.Rows[0][0].ToString()) > 0)
{
    uniqueContactNumber = false;
}
else
{
    uniqueContactNumber = true;
}