将monthcalendar中的日期与循环添加到mdb

时间:2015-11-03 12:15:30

标签: c# database ms-access oledb

我目前正在尝试使用monthcalendar将多个日期添加到Access MdB中。

这个想法是,每个选定的日期都是插入数据库的条目。

这是我用来获取我的monthcalendar范围的代码:

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但我不确定如何。< / p>

我真的很感激每一个有用的答案。 在此先感谢大家。 :)

2 个答案:

答案 0 :(得分:0)

You've got it pretty close. Just add a couple of DateTime variables on the form to store startDay and endDay.

public class Form1 : Form
{
    private DateTime _startDate {get; set;}
    private DateTime _endDate {get; set;}

    // your other code
}

And assign them when then user selects a date range:

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); 

_StartDate = startDay;
_endDate = endDay;

while (startDay < endDay)
{
    DateTime Day = startDay.AddDays(1);
    string Day1 = Day.ToShortDateString();
}

}

And then you can access them in your button's click method:

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)
}

答案 1 :(得分:0)

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

  //_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
         }
    }

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