来访人员登记系统(四)预约登记模块

添加记录功能

填写表单后,通过单击“添加记录”按钮提交预约信息,在弹出的提示框中核对正确信息后,记录到数据库中。

private void button_add_Click(object sender, EventArgs e)
{
    string name = textBox_add_name.Text;
    string company = textBox_add_company.Text;
    string phone = textBox_add_phone.Text;
    string idcard = textBox_add_idcard.Text;
    string staff = textBox_add_staff.Text;
    string ETA = textBox_add_ETA.Text;
    string date = dateTimePicker_add.Value.ToString("yyyy-MM-dd");
    string relevants = textBox_add_relevants.Text;
    string intention = textBox_add_intention.Text;
         
    if (MessageBox.Show("您确定要添加下列数据吗?\n姓名:" + name + "\n单位:" + company + "\n身份证号:" + idcard + 
                        "\n手机:" + phone + "\n陪同人员:" + staff + "\n预约时间:" + ETA + "\n预约日期:" + date + "\n涉及设备/系统:" +
                        relevants + "\n来访目的:" + intention, "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
    {
        try
        {
            // 查看表中是否已有预约(身份证号、预约日期、来访目的三个字段都雷同)
            string cmd = "select * from registration where idcard='" + textBox_add_idcard.Text + "' and date='" +
                         dateTimePicker_add.Value.ToString("yyyy-MM-dd") + "' and intention='" + textBox_add_intention.Text + "';";

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

            // 没有重复预约,插入新的预约信息
            if (!dr.Read())
            {
                dr.Close();
                // 插入数据
                cmd = "insert into registration (name, company, phone, idcard, staff, ETA, date, relevants, intention" +
                      ") values('" + textBox_add_name.Text + "','" + textBox_add_company.Text + "','" + 
                      textBox_add_phone.Text + "','" + textBox_add_idcard.Text + "','" + textBox_add_staff.Text + "','" +
                      textBox_add_ETA.Text + "','" + dateTimePicker_add.Value.ToString("yyyy-MM-dd") + "','" + 
                      textBox_add_relevants.Text + "','" + textBox_add_intention.Text + "');";

                SQLCmd = new MySqlCommand(cmd, DBconn);
                SQLCmd.ExecuteNonQuery();
                MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK);

                // 插入完成后刷新表格
                button_add_reset_Click(sender, e);
            }

            // 包含重复预约,插入失败,定位到重复预约的位置
            else
            {
                dr.Close();
                MessageBox.Show("已有预约,添加失败", "提示", MessageBoxButtons.OK);
                int row = dataGridView1.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    // 第3列、第6列、第8列分别对应身份证号、预约日期和来访目的
                    if (textBox_add_idcard.Text == dataGridView1.Rows[i].Cells[3].Value.ToString() &&
                        dateTimePicker_add.Value.ToString("yyyy/M/d") + " 0:00:00" == dataGridView1.Rows[i].Cells[6].Value.ToString() &&
                        textBox_add_intention.Text == dataGridView1.Rows[i].Cells[8].Value.ToString())
                    {
                        dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[1];
                        dataGridView1.CurrentRow.Selected = true;
                    }
                }
            }
        }
        catch (MySqlException ex)
        {
            MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
        }
    }
}

从客户端添加记录

在来访人员登记系统(二)中讨论了服务器和客户端之间通过websocket通信,最后一步是把客户端传来的信息发送到addfromClient方法,再通过此方法提交至数据库,下面代码实现了addfromClient方法。

private void addfromClient(string msg)
{
    string name = msg.Split(',')[0];
    string staff = msg.Split(',')[1];
    string company = msg.Split(',')[2];
    string idcard = msg.Split(',')[3];
    string phone = msg.Split(',')[4];
    string date = msg.Split(',')[5];
    string ETA = msg.Split(',')[6];
    string relevants = msg.Split(',')[7];
    string intention = msg.Split(',')[8];

    try
    {
        // 查看表中是否已有预约(身份证号、预约日期、来访目的三个字段都雷同)
        string cmd = "select * from registration where idcard='" + idcard + "' and date='" +
                     date + "' and intention='" + intention + "';";

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

        // 没有重复预约,插入新的预约信息
        if (!dr.Read())
        {
            dr.Close();
            // 插入数据
            cmd = "insert into registration (name, company, phone, idcard, staff, ETA, date, relevants, intention" +
                  ") values('" + name + "','" + company + "','" + phone + "','" + idcard + "','" + staff + "','" +
                  ETA + "','" + date + "','" + relevants + "','" + intention + "');";

            SQLCmd = new MySqlCommand(cmd, DBconn);
            SQLCmd.ExecuteNonQuery();
        }

        else
        {
            dr.Close();
        }
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK);
    }
}

总结

C#字符串的Split方法将一个字符串以指定字符为分界点,分隔成若干个字符串,并可以通过中括号访问;
dateTimePicker.Value.ToString()方法中可以添加参数,规定转换时的时间格式。

posted @ 2020-09-14 15:37  老鼠司令  阅读(456)  评论(0)    收藏  举报