如何获取月份列表并将其插入C#中的数据库?

问题描述:

大家好,



我需要获得月份并将其插入数据库。我有两个datetimepickers。第一个是开始日期,另一个是结束日期。当我点击按钮时,它将根据两个datetimepicker获取月份列表并将其保存到数据库。



这是我尝试过的:(我在.ParseExact中出现错误,说parseExact没有重载方法接受一个参数)。我不明白。这是我第一次使用这个日期时间:(





Hello guys,

I need to get the months and insert it to the database. I have two datetimepickers. the first one is the start date and the other one is the end date. When I click the button, it will get the list of months based from the two datetimepicker and save it to the database.

Here is what I have tried:(I got an error in .ParseExact saying no overload method for parseExact takes one argument). I do not understand this. It is my first time to work with this datetime :(


public static IEnumerable<Tuple<string, int>> MonthsBetween(
        DateTime startDate,
        DateTime endDate)
     {

         DateTime iterator;
         DateTime limit;

         if (endDate > startDate)
         {
             iterator = new DateTime(startDate.Year, startDate.Month, 1);
             limit = endDate;
         }
         else
         {
             iterator = new DateTime(endDate.Year, endDate.Month, 1);
             limit = startDate;
         }

         var dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;
         while (iterator <= limit)
         {
             yield return Tuple.Create(
                 dateTimeFormat.GetMonthName(iterator.Month),
                 iterator.Year);
             iterator = iterator.AddMonths(1);
         }
     }










private void button1_Click_1(object sender, EventArgs e)
    {
        string a = dateTimePicker1.ToString();
        string b =dateTimePicker2.ToString();

        var startDate = DateTime.ParseExact(a);
        var endDate = DateTime.ParseExact(b);

        var months = MonthsBetween(startDate, endDate);
    }

你的函数返回一个Enumerable< Tuple< string,int>>。



你只需要调用具有DateTimePicker控件的Value属性的函数:



var months = MonthsBetween(dateTimePicker1.Value,dateTimePicker2.Value);



这是一种替代方法,你可以在for循环中使用DateTime来解决这个问题:
There's nothing wrong with your function that's returning an Enumerable<Tuple<string,int>>.

You just need to call the function with the 'Value property of the DateTimePicker Controls:

var months = MonthsBetween(dateTimePicker1.Value, dateTimePicker2.Value);

Here's an alternative way you could approach this using DateTime in a for-loop:
private DateTimeFormatInfo dateTimeFormat = CultureInfo.CurrentCulture.DateTimeFormat;

private List<Tuple<string, int>> BetweenMonths = new List<Tuple<string, int>>();

private void button1_Click(object sender, EventArgs e)
{
    DateTime startDate = dateTimePicker1.Value;
    DateTime endDate = dateTimePicker2.Value;

    if (endDate < startDate)
    {
        DateTime temp = endDate;
        endDate = startDate;
        startDate = temp;
    }

    BetweenMonths.Clear();

    for (DateTime date = startDate; date < endDate; date = date.AddMonths(1))
    {
        BetweenMonths.Add(Tuple.Create(dateTimeFormat.GetMonthName(date.Month), date.Year));
    }
}