C#使用2个日期的数据填充数据库

时间:2017-03-11 12:47:26

标签: c# insert access

我正在创建一个考勤监控系统,它可以设置休假,但只有一个日期,我想要的输出是我可以在两个日期设置休假。例如从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);
        }
    }

1 个答案:

答案 0 :(得分:0)

您需要引入另一个日期选择器,以便用户可以选择FromTo日期休假。

然后你需要遍历日期并调用你的方法:

var fromDate = dateTimePickerFrom.Value;
var toDate = dateTimePickerTo.Value;
for(DateTime offDate = fromDate; offDate.Date <= toDate.Date; offDate = offDate.AddDays(1))
{
    SetLeave(offDate);
}

对您的方法进行以下更改:

private void Setleave(DateTime offDate)
{
    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);

    // This is the changed line
    command.Parameters.AddWithValue("@4", offDate.ToShortDateString());

    command.Parameters.AddWithValue("@5", textBox2.Text);
    command.ExecuteNonQuery();
    MessageBox.Show("Data Saved!");
}

尝试并使用.NET命名约定。我已将您的方法名称从setLeave更改为SetLeave,以遵循将Pascal大小写用于方法名称的惯例。

此外,请进行验证以确保用户不会在To日期选择器中选择比From日期选择器中选择的值更早的日期。