-1

我有一个问题,我似乎无法弄清楚。我正在创建一个添加客户信息的应用程序,然后将其与正在提供的服务相关联。我在 xampp 数据库中创建了两个表之间的关系。这些表是 tclientinfo 和 tserviceinfo。tclientinfo 表中的列是 Client_ID(PK)、First_Name、Last_Name、Phone 和 Email。在 tserviceinfo 中,Service_Order、Date、Tech、Brand、Model、OS、Type、PC_Issue、Service、Service_Price、Total_Price、Service_Notes、Client_ID。Client_ID 是我使用的关系,但我无法将信息添加到 tserviceinfo 表。我收到 SQL 异常:无法添加或更新子行:外键约束失败('dbocomputerinformation'.'tserviceinfo',CONSTRAINT 'tserviceinfo_ibfk_3'

我将信息插入 tclientinfo 的添加按钮是:

private void btnAdd_Click(object sender, EventArgs e) { MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinformation;"); conn.Open();

        string email = tbEmail.Text;
        string date = tbDate.Text;

        //try statement for validation before sending to database. 
        try
        {
            if (tbFirstName.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's first name.");
                this.tbFirstName.Focus();
                return;
            }
            if (tbLastName.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's last name.");
                this.tbLastName.Focus();
                return;
            }
            if (tbPhone.Text.Length < 10)
            {
                MessageBox.Show("Phone number must be ten digits.");
                this.tbPhone.Focus();
                return;
            }
            if (tbPhone.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's phone number.");
                this.tbPhone.Focus();
                return;
            }
            if (tbPhone.Text != string.Empty && !Regex.IsMatch(tbPhone.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Please enter in numbers only.");
                this.tbPhone.Focus();
                return;
            }
            if (tbEmail.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's email.");
                this.tbEmail.Focus();
                return;
            }
            if (email.IndexOf('@') == -1 || email.IndexOf('.') == -1)
            {
                MessageBox.Show("Please enter a valid email address.");
                this.tbEmail.Focus();
                return;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            MySqlCommand command = new MySqlCommand("Insert into tclientinfo (First_Name, Last_Name, Phone, Email) values (?First_Name, ?Last_Name, ?Phone, ?Email)", conn);
            command.Parameters.AddWithValue("?First_Name", tbFirstName.Text);
            command.Parameters.AddWithValue("?Last_Name", tbLastName.Text);
            command.Parameters.AddWithValue("?Phone", tbPhone.Text);
            command.Parameters.AddWithValue("?Email", tbEmail.Text);

            command.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("Client Added Successfully.");

        }
        catch (MySqlException ex)
        {
            MessageBox.Show("Can't connect to database\n" + ex.ToString());
        }
    }

然后我插入 tserviceinfo 是:

private void btnCreate_Click(object sender, EventArgs e) { //string connString = "server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinfo;"; MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinfo;"); conn.Open();

        string date = tbDate.Text;

        //try statement for validation before sending to database. 
        try
        {
            if (tbDate.Text.Length == 0)
            {
                MessageBox.Show("Please enter in date of service.");
                this.tbDate.Focus();
                return;
            }
            if (date.IndexOf('-') == -2) 
            {
                MessageBox.Show("Please enter correct date. mm-dd-yy.");
                this.tbDate.Focus();
                return;
            }
            if (tbTech.Text.Length == 0)
            {
                MessageBox.Show("Please enter in service technician.");
                this.tbTech.Focus();
                return;
            }
            if (tbBrand.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's brand of computer.");
                this.tbBrand.Focus();
                return;
            }
            if (tbModel.Text.Length == 0)
            {
                MessageBox.Show("Please enter in computer model.");
                this.tbModel.Focus();
                return;
            }
            if (tbOS.Text.Length == 0)
            {
                MessageBox.Show("Please enter in computer's operating system.");
                this.tbOS.Focus();
                return;
            }
            if (rbDesktop.Checked == false && rbNotebook.Checked == false)
            {
                MessageBox.Show("Please select device type.");
                this.rbDesktop.Focus();
                return;
            }
            if (lvOrdered.Items.Count == 0)
            {
                MessageBox.Show("Please select a service.");
                this.lvServices.Focus();
                return;
            }
            if (tbIssue.Text.Length == 0)
            {
                MessageBox.Show("Please describe the issue with the computer.");
                this.tbIssue.Focus();
                return;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            MySqlCommand command = new MySqlCommand("Insert into tserviceinfo (Date, Tech, Brand, Model, OS, Type, PC_Issue, Service, Service_Price, Total_Price) values (?Date, ?Tech, ?Brand, ?Model, ?OS, ?Type, ?PC_Issue, ?Service, ?Service_Price, ?Total_Price)", conn);
            command.Parameters.AddWithValue("?Date", tbDate.Text);
            command.Parameters.AddWithValue("?Tech", tbTech.Text);
            command.Parameters.AddWithValue("?Brand", tbBrand.Text);
            command.Parameters.AddWithValue("?Model", tbModel.Text);
            command.Parameters.AddWithValue("?OS", tbOS.Text);
            command.Parameters.AddWithValue("?PC_Issue", tbIssue.Text);

            /**
             * Was having issues inserting a single service, it would dublicate itself in the db, this was the solution
             * this adds just a single service with nothing else, and it reads back properly as well. 
            **/
            if (lvOrdered.Items.Count == 1)
            {
                string singleService = string.Join(" ", services);
                command.Parameters.AddWithValue("?Service", singleService);
            }
            else
            {
                /**
                * Combine all the services into one string seperated by commas
                * and add that to the database
                * */
                string serviceList = string.Join(", ", services);
                command.Parameters.AddWithValue("?Service", serviceList);

            }
            /**
             * Combine all the prices into one string seperated by commas
             * and add that to the database
             * */
            string servicePriceList = string.Join(", ", servicePrice);
            command.Parameters.AddWithValue("?Service_Price", servicePriceList);
            command.Parameters.AddWithValue("?Total_Price", lblPrice.Text);

            //clears lists so dublicates don't happen when creating more work orders than just 1.
            services.Clear();
            servicePrice.Clear();

            if (rbDesktop.Checked)
            {
                command.Parameters.AddWithValue("?Type", "Desktop");
            }
            else if (rbNotebook.Checked)
            {
                command.Parameters.AddWithValue("?Type", "Notebook");
            }

            command.ExecuteNonQuery();
            conn.Close();

            tbFirstName.Clear();
            tbLastName.Clear();
            tbDate.Clear();
            tbEmail.Clear();
            tbBrand.Clear();
            tbIssue.Clear();
            tbTech.Clear();
            tbModel.Clear();
            tbPhone.Clear();
            tbOS.Clear();
            lvOrdered.Items.Clear();
            lblPrice.Text = "$0.00";

        }
        catch (MySqlException ex)
        {
            MessageBox.Show("Can't connect to database\n" + ex.ToString());
        }
    }

所以不用说,我不知道从这里做什么。任何帮助都会很棒或任何想法。如果您需要更多信息,请告诉我。谢谢!

4

1 回答 1

0

想通了,对不起,笨蛋。但我需要选择 Client_ID 并将其存储为变量。然后将其插入到第二个表中。

所以如果其他人有这个问题,就像这样。

intMaxClientId;

MySqlCommand command = new MySqlCommand("Select max(Client_ID) from tclientinfo", conn); maxClientId = Convert.ToInt32(command.ExecuteScalar());

command.Parameters.AddWithValue("?Client_ID", maxClientId);

于 2013-03-06T22:55:23.910 回答