MySQL插入/更新查询

时间:2014-04-14 17:28:35

标签: c# mysql

原始问题

我有一个表单:PICTURE

表单从父菜单中的ComboBox获取值,并运行一个查询,返回包含该JobNumber的所有行,然后使用相关值填充左侧的Items。

当您点击“保存表单”时,表单会运行一个MySQL查询,查找重复键并在找到时进行更新,否则,它会将值添加到新行。 (见下面的代码)

但是,我需要以某种方式添加Where子句,查看JobNumber和Revision ComboBoxes以允许选择,创建新修订和更新修订。我不认为可以通过"插入...重复密钥更新"查询,但我不确定我需要做些什么来使它工作。

任何建议?

另外,我的表格如下:PICTURE

这是我查询的代码。 (表格上的相应项目应该是非常自我解释的。)

var cmd = new MySqlCommand("Insert into pricingTemplate" + 
                "(ID, JobNumber, Date, Size, Material1, Material2, Material3, Material4, Check1, Check1Option, Check2, Check2Option, Revision) " +
                "SELECT DISTINCT " +
                "'" + txtID.Text + "', " +
                "'" + cmbJobNumber.Text + "', " +
                "'" + txtDate.Text + "', " +
                "'" + cmbSize.Text + "', " +
                "'" + txtMaterial1.Text + "', " +
                "'" + txtMaterial2.Text + "', " +
                "'" + txtMaterial3.Text + "', " +
                "'" + txtMaterial4.Text + "', " +
                "'" + check1checked + "', " +
                "'" + check1Optionchecked + "', " +
                "'" + check2checked + "', " +
                "'" + check2Optionchecked + "', " +
                "'" + cmbRevision.Text + "' " +
                "on duplicate key update" +
                " ID= '" + txtID.Text + "', " + 
                " Date= '" + txtDate.Text + "', " +
                " Size= '" + cmbSize.Text + "', " +
                " Material1= '" + txtMaterial1.Text + "', " +
                " Material2= '" + txtMaterial2.Text + "', " +
                " Material3= '" + txtMaterial3.Text + "', " +
                " Material4= '" + txtMaterial4.Text + "', " +
                " Check1= '" + check1checked + "', " +
                " Check1Option= '" + check1Optionchecked + "', " +
                " Check2= '" + check2checked + "', " +
                " Check2Option= '" + check2Optionchecked + "', " +
                " Revision= '" + cmbRevision.Text + "';", con);

            int rowsAffected = cmd.ExecuteNonQuery();

            if (rowsAffected > 0)
            {
                // Confirm that the Job has been saved
                MessageBox.Show("The Job has been saved",
                    "Saved",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
                DataConnection(lblJob.Text);
            }
            else
            {
                // Throw Error
                MessageBox.Show("An Error has occurred!",
                    "NOT Saved",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
                DataConnection(lblJob.Text);
            }      

更新

这是我现在的代码。它有效,但我觉得有更好的方法来完成同样的任务。

    private void btnUpdateJob_Click(object sender, EventArgs e)
    {
        if      ( txtID.Text == ""  )
                { newjob();         }
        else    {                   }

        //  Save Job Data to Database
        DialogResult result;

        result = MessageBox.Show("You are editing Job " + lblJob.Text + "\n" + "Are you sure you want to update this job?",
            "Are you Sure?",
            MessageBoxButtons.YesNo,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button1);

        if (result == DialogResult.Yes)
        {
            string check1checked;
            string check1Optionchecked;
            string check2checked;
            string check2Optionchecked;

            if      (   check1.Checked          ==  true )
                    {   check1checked           =   "1"; }
            else    {   check1checked           =   "0"; }

            if      (   check1Option.Checked    ==  true )
                    {   check1Optionchecked     =   "1"; }
            else    {   check1Optionchecked     =   "0"; }

            if      (   check2.Checked          ==  true )
                    {   check2checked           =   "1"; }
            else    {   check2checked           =   "0"; }

            if      (   check2Option.Checked    ==  true )
                    {   check2Optionchecked     =   "1"; }
            else    {   check2Optionchecked     =   "0"; }

            //  Database Connection String

            string server   =   "192.168.1.149";
            string database =   "starflitesystems";
            string userid   =   "iuapp";
            string password =   "iuapp";
            string str      =   "SERVER="   + server    + ";" + 
                                "DATABASE=" + database  + ";" +
                                "UID="      + userid    + ";" + 
                                "PASSWORD=" + password  + ";";

            MySqlConnection myconn2 = new MySqlConnection(str);
            MySqlConnection con     = new MySqlConnection(str);
            con.Open();

            // Insert Form data into Database if it does not exist

            var cmd = new MySqlCommand("Insert into pricingTemplate" +          
                "(ID, JobNumber, Date, Size, Material1, Material2, Material3, Material4, Check1, Check1Option, Check2, Check2Option, Revision) " +
                "select " +
                "'" + txtID.Text            + "', " +     
                "'" + cmbJobNumber.Text     + "', " +
                "'" + txtDate.Text          + "', " +
                "'" + cmbSize.Text          + "', " +
                "'" + txtMaterial1.Text     + "', " +
                "'" + txtMaterial2.Text     + "', " +
                "'" + txtMaterial3.Text     + "', " +
                "'" + txtMaterial4.Text     + "', " +
                "'" + check1checked         + "', " +
                "'" + check1Optionchecked   + "', " +
                "'" + check2checked         + "', " +
                "'" + check2Optionchecked   + "', " +
                "'" + cmbRevision.Text      + "' from pricingtemplate " +
                "WHERE NOT EXISTS(SELECT * FROM pricingtemplate WHERE " + 
                "JobNumber= '"  + cmbJobNumber.Text + "' AND " + 
                "Revision= '"   + cmbRevision.Text  + "') Limit 1;", con);

            //Run First Query
            int rowsAffected = cmd.ExecuteNonQuery();

            if ( rowsAffected > 0 )
            {
                // Show Query Succeded MessageBox
                MessageBox.Show("Query Succeded!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);


                // Reset the dataGridView
                DataConnection(lblJob.Text);
            }

            else
            {
                newjob();

                // Show Query Failed MessageBox
                MessageBox.Show("Query Failed!", "Failure", MessageBoxButtons.OK, MessageBoxIcon.Error);


                // Run Second Query
                var cmd2 = new MySqlCommand("Update pricingtemplate " + 
                    "set " + 
                    "JobNumber='"       + cmbJobNumber.Text     + "', "     +
                    "Revision='"        + cmbRevision.Text      + "', "     +
                    "Date='"            + txtDate.Text          + "', "     +
                    "Size='"            + cmbSize.Text          + "', "     +
                    "Material1='"       + txtMaterial1.Text     + "', "     +
                    "Material2='"       + txtMaterial2.Text     + "', "     +
                    "Material3='"       + txtMaterial3.Text     + "', "     +
                    "Material4='"       + txtMaterial4.Text     + "', "     +
                    "Check1='"          + check1checked         + "', "     +
                    "Check1Option='"    + check1Optionchecked   + "', "     +
                    "Check2='"          + check2checked         + "', "     +
                    "Check2Option='"    + check2Optionchecked   + "' "      +
                    "Where JobNumber='" + cmbJobNumber.Text     + "' and "  + 
                    "Revision='"        + cmbRevision.Text      + "' Limit 1;", con);

                cmd2.ExecuteNonQuery();

                // Reset the dataGridView
                DataConnection(lblJob.Text);
            }
        }

1 个答案:

答案 0 :(得分:1)

您是否研究过复合重复密钥更新,显然如果您将多个列设置为主键,它可以正常工作。