来访人员登记系统(六)修改预约信息模块

修改记录功能

实现:选中datagridview中的一条记录,在数据库中按照文本框中输入的内容进行修改,如果某一个文本框没有输入或者输入的内容与之前一致,则不对选中记录的该字段进行改动。

private void button_alter_add_Click(object sender, EventArgs e)
{
    string value = dataGridView3.CurrentRow.Cells[0].Value.ToString();
    int number = Convert.ToInt32(value);
    string cmd;
    MySqlCommand SQLCmd;

    string name;
    string name_old;
    string company;
    string company_old;
    string idcard;
    string idcard_old;
    string phone;
    string phone_old;
    string staff;
    string staff_old;
    string ETA;
    string ETA_old;
    string date;
    string date_old;
    string relevants;
    string relevants_old;
    string intention;
    string intention_old;

    try
    {
        cmd = "select name, company, idcard, phone, staff, ETA, date, relevants, intention from registration where " +
              "number=" + number + ";";

        SQLCmd = new MySqlCommand(cmd, DBconn);
        MySqlDataReader dr = SQLCmd.ExecuteReader();
        dr.Read();

        name_old = dr.GetString(0);
        company_old = dr.GetString(1);
        idcard_old = dr.GetString(2);
        phone_old = dr.GetString(3);
        staff_old = dr.GetString(4);
        ETA_old = dr.GetString(5);
        date_old = dr.GetString(6).Split(' ')[0];
        relevants_old = dr.GetString(7);
        intention_old = dr.GetString(8);

        dr.Close();

        if (textBox_alter_name.Text != dataGridView3.CurrentRow.Cells[1].Value.ToString() && 
            textBox_alter_name.Text != "姓名")
        {
            name = textBox_alter_name.Text;
        }
        else
        {
            name = "不变";
        }
        if (textBox_alter_company.Text != dataGridView3.CurrentRow.Cells[2].Value.ToString() &&
            textBox_alter_company.Text != "单位")
        {
            company = textBox_alter_company.Text;
        }
        else
        {
            company = "不变";
        }
        if (textBox_alter_idcard.Text != dataGridView3.CurrentRow.Cells[3].Value.ToString() && 
            textBox_alter_idcard.Text != "身份证号")
        {
            idcard = textBox_alter_idcard.Text;
        }
        else
        {
            idcard = "不变";
        }
        if (textBox_alter_phone.Text != dataGridView3.CurrentRow.Cells[4].Value.ToString() && 
            textBox_alter_phone.Text != "电话")
        {
            phone = textBox_alter_phone.Text;
        }
        else
        {
            phone = "不变";
        }
        if (textBox_alter_staff.Text != dataGridView3.CurrentRow.Cells[5].Value.ToString() &&
            textBox_alter_staff.Text != "陪同人员")
        {
            staff = textBox_alter_staff.Text;
        }
        else
        {
            staff = "不变";
        }
        if (textBox_alter_ETA.Text != dataGridView3.CurrentRow.Cells[6].Value.ToString() &&
            textBox_alter_ETA.Text != "预约时间")
        {
            ETA = textBox_alter_ETA.Text;
        }
        else
        {
            ETA = "不变";
        }
        if (dateTimePicker_alter.Value.ToString("yyyy/M/d") + " 0:00:00" != dataGridView3.CurrentRow.Cells[7].Value.ToString())
        {
            date = dateTimePicker_alter.Value.ToString("yyyy/M/d");
        }
        else
        {
            date = "不变";
        }
        if (textBox_alter_relevants.Text != dataGridView3.CurrentRow.Cells[8].Value.ToString() &&
            textBox_alter_relevants.Text != "涉及设备/系统")
        {
            relevants = textBox_alter_relevants.Text;
        }
        else
        {
            relevants = "不变";
        }
        if (textBox_alter_intention.Text != dataGridView3.CurrentRow.Cells[9].Value.ToString() && 
            textBox_alter_intention.Text != "来访目的")
        {
            intention = textBox_alter_intention.Text;
        }
        else
        {
            intention = "不变";
        }

        // 修改
        if (MessageBox.Show("您确定要应用如下更改?\n" +
            "姓名:" + name_old + "  -->  " + name + "\n" +
            "单位:" + company_old + "  -->  " + company + "\n" +
            "身份证号:" + idcard_old + "  -->  " + idcard + "\n" +
            "电话:" + phone_old + "  -->  " + phone + "\n" +
            "陪同人员:" + staff_old + "  -->  " + staff + "\n" +
            "预约时间:" + ETA_old + "  -->  " + ETA + "\n" +
            "预约日期:" + date_old + "  -->  " + date + "\n" +
            "涉及设备/系统:" + relevants_old + "  -->  " + relevants + "\n" +
            "来访目的:" + intention_old + "  -->  " + intention, "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
        {
            if (textBox_alter_name.Text != dataGridView3.CurrentRow.Cells[1].Value.ToString() &&
                textBox_alter_name.Text != "姓名")
            {
                cmd = "update registration set name='" + textBox_alter_name.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_company.Text != dataGridView3.CurrentRow.Cells[2].Value.ToString() &&
                textBox_alter_company.Text != "单位")
            {
                cmd = "update registration set company='" + textBox_alter_company.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_idcard.Text != dataGridView3.CurrentRow.Cells[3].Value.ToString() &&
                textBox_alter_idcard.Text != "身份证号")
            {
                cmd = "update registration set idcard='" + textBox_alter_idcard.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_phone.Text != dataGridView3.CurrentRow.Cells[4].Value.ToString() &&
                textBox_alter_phone.Text != "电话")
            {
                cmd = "update registration set phone='" + textBox_alter_phone.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_staff.Text != dataGridView3.CurrentRow.Cells[5].Value.ToString() &&
                textBox_alter_staff.Text != "陪同人员")
            {
                cmd = "update registration set staff='" + textBox_alter_staff.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_ETA.Text != dataGridView3.CurrentRow.Cells[6].Value.ToString() &&
                textBox_alter_ETA.Text != "预约时间")
            {
                cmd = "update registration set ETA='" + textBox_alter_ETA.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (dateTimePicker_alter.Value.ToString("yyyy/M/d") + " 0:00:00" != dataGridView3.CurrentRow.Cells[7].Value.ToString())
            {
                cmd = "update registration set date='" + dateTimePicker_alter.Value.ToString("yyyy-MM-dd") + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_relevants.Text != dataGridView3.CurrentRow.Cells[8].Value.ToString() &&
                textBox_alter_relevants.Text != "涉及设备/系统")
            {
                cmd = "update registration set intention='" + textBox_alter_relevants.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }

            if (textBox_alter_intention.Text != dataGridView3.CurrentRow.Cells[9].Value.ToString() &&
                textBox_alter_intention.Text != "来访目的")
            {
                cmd = "update registration set intention='" + textBox_alter_intention.Text + "' where number=" + number + ";";
                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
            }
            MessageBox.Show("修改完成", "提示", MessageBoxButtons.OK);
            button_alter_reset_Click(sender, e);
        }
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
    }
}

总结

修改之前需要对每个输入内容进行逐个验证,看看是否为空或者和数据库中内容一致,修改时需要用户进行信息的确认。

posted @ 2020-09-14 16:21  老鼠司令  阅读(322)  评论(0)    收藏  举报