【发布时间】:2017-08-01 19:14:57
【问题描述】:
我正在创建一个考勤监控系统,它可以设置休假,但只有一个日期,我想要的输出是我可以设置两个日期的休假。例如,从 3 月 6 日到 3 月 9 日,它会在数据库中插入值是 march6、march 7、march8 和 9 的休假。每个日期都会创建一个添加行
这是我的代码:
private void setleave()
{
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = "INSERT INTO [TimeinTimeout](EmployeeID, Firstname, Lastname, InDate, Remarks) VALUES (@1,@2,@3,@4,@5)";
command.Parameters.Clear();
command.Parameters.AddWithValue("@1", textBox1.Text);
command.Parameters.AddWithValue("@2", dataGridView1.Rows[0].Cells[1].Value);
command.Parameters.AddWithValue("@3", dataGridView1.Rows[0].Cells[2].Value);
command.Parameters.AddWithValue("@4", dateTimePicker1.Value.ToShortDateString());
command.Parameters.AddWithValue("@5", textBox2.Text);
command.ExecuteNonQuery();
MessageBox.Show("Data Saved!");
}
private void button1_Click(object sender, EventArgs e) { 尝试 {
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = @" SELECT EmployeeID, Firstname, Lastname
FROM tblEmployee
WHERE EmployeeID = @1";
command.Parameters.AddWithValue("@1", textBox1.Text);
var fromDate = dateTimePickerFrom.Value;
var toDate = dateTimePickerTo.Value;
for(DateTime offDate = fromDate; offDate.Date <= toDate.Date; offDate = offDate.AddDays(1))
{
SetLeave(offDate);
}
reader = command.ExecuteReader();
if (reader.Read())
{
if (MessageBox.Show("Are you sure you want to set Employee as Leave?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
{
dataGridView1.Rows[0].Cells[0].Value = reader[0].ToString();
dataGridView1.Rows[0].Cells[1].Value = reader[1].ToString();
dataGridView1.Rows[0].Cells[2].Value = reader[2].ToString();
dataGridView1.Rows[0].Cells[3].Value = dateTimePicker1.Value.ToString();
setleave();
}
}
else
{
MessageBox.Show("Invalid Employee ID!");
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
【问题讨论】: