-1

わからないみたいな質問があります。クライアントの情報を追加し、提供されているサービスに関連付けるアプリケーションを作成しています。xampp データベースで 2 つのテーブル間の関係を作成しました。テーブルは 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 を選択して変数として保存する必要がありました。次に、2 番目のテーブルに挿入します。

他の誰かがこの質問をしたことがある場合は、このようにします。

intMaxClientId;

MySqlCommand command = new MySqlCommand("tclientinfo から max(Client_ID) を選択", conn); maxClientId = Convert.ToInt32(command.ExecuteScalar());

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

于 2013-03-06T22:55:23.910 に答える