将带有循环的月历中的日期添加到mdb

本文关键字:日期 添加 mdb 月历 循环 | 更新日期: 2023-09-27 18:20:17

我目前正试图使用月历将多个日期添加到Access MdB中。

其想法是,对于每个选定的日期,都将有一个条目插入到数据库中。

这是我用来从我的月历中获取范围的代码:

public void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e)
    {
        string start = monthCalendar1.SelectionRange.Start.ToShortDateString();
        string end = monthCalendar1.SelectionRange.End.ToShortDateString();
        DateTime startDay = Convert.ToDateTime(start); 
        DateTime endDay = Convert.ToDateTime(end); 
        while (startDay < endDay)
        {
            DateTime Day = startDay.AddDays(1);
            string Day1 = Day.ToShortDateString();}}

按下按钮时,应在每个选定日期执行此代码:

public void btn_speichern_Click(object sender, EventArgs e)    
          {
                 if (checkBox2.Checked == true)
         {
                //con.Open();
                string Vacation = "Vacation";
                command.Connection = con;
                command.CommandText = "insert into " + user + " (Date, Worktime) values ('" + dateTimePicker1.Text + "', '" + Vacation + "')";
                command.ExecuteNonQuery();
                string query = "select * from " + user;
                command.CommandText = query;
                OleDbDataAdapter Daten = new OleDbDataAdapter(command);
                DataTable Datenquelle = new DataTable();
                Daten.Fill(Datenquelle);
                dataGridView1.DataSource = Datenquelle;
                dataGridView1.AllowUserToResizeColumns = true;
                dataGridView1.AllowUserToResizeRows = true;
                dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                int totalRowHeight = dataGridView1.ColumnHeadersHeight;
                foreach (DataGridViewRow row in dataGridView1.Rows)
                    totalRowHeight += row.Height;
                dataGridView1.Height = totalRowHeight;
                this.Height = dataGridView1.Height + 100;
                con.Close();
            }
            }

但我不知道如何在选择的每个日期执行代码,直到它到达结束日期。。。。很确定我必须更改datetimepicker1.text,但我不确定如何更改。

我真的很感激每一个有用的答案。提前感谢大家。:)

将带有循环的月历中的日期添加到mdb

您已经非常接近了。只需在表单上添加几个DateTime变量即可存储startDayendDay

public class Form1 : Form
{
    private DateTime _startDate {get; set;}
    private DateTime _endDate {get; set;}
    // your other code
}

然后当用户选择日期范围时分配它们:

public void monthCalendar1_DateSelected(对象发件人,DateRangeEventArgs e){

string start = monthCalendar1.SelectionRange.Start.ToShortDateString();
string end = monthCalendar1.SelectionRange.End.ToShortDateString();
DateTime startDay = Convert.ToDateTime(start); 
DateTime endDay = Convert.ToDateTime(end); 
_StartDate = startDay;
_endDate = endDay;
while (startDay < endDay)
{
    DateTime Day = startDay.AddDays(1);
    string Day1 = Day.ToShortDateString();
}

}

然后你可以通过点击按钮的方法访问它们:

public void btn_speichern_Click(object sender, EventArgs e)    
{
    do 
    {
        //probably want a do..while here, so if they select a single day it still goes once.
        //now do your other stuff, and then increment _startDate
        _startDate = _startDate.AddDays(1);
    }
    while (_startDate <= _endDate)
}

我建议您对日期(以及其他所有内容)使用参数化查询。会帮你省去很多麻烦。您可以为自己编写一种方法,它获取字典中的值,然后加载命令参数。这是一个日期的简单例子。

  //_date is your date value and conS is your connection string
    public int InsertDate(DateTime _date, string conS) 
    {
          try
          {
                using (var cn = new OleDbConnection(conS))
                {
                      string queryString = "INSERT INTO... some_date_field=@dateparam"; //your query
                      var cmd = new OleDbCommand(queryString, cn);              
                      cmd.Parameters
                      .Add(new OleDbParameter("@dateparam", _date)); // add the parameter
                      cn.Open();
                      int res = cmd.ExecuteNonQuery();
                      return res;
                 }
         }
         catch
         {
               return -2; // if you get that you will know something has gone wrong
         }
    }

这只是插入日期字段的演示。