找出所有奇数星期六并将它们插入数据库

本文关键字:插入 数据库 星期六 | 更新日期: 2023-09-27 18:11:09

我需要找到所有奇数星期六日期并将它们插入数据库。我已经为所有的星期六写了代码,但不知道一个月中只有奇数个星期六该怎么做。

    int year = Convert.ToInt32(StartYeartxt.Text);
    int month = 1;
    System.Globalization.CultureInfo currentCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
    for (month = 1; month <= 12; month++)
    {
        for (int i = 1; i <= currentCulture.Calendar.GetDaysInMonth(year, month); i++)
        {
            DateTime d = new DateTime(year, month, i);
            if (d.DayOfWeek == day)
            {
                con = OpenCon.OpenConnection();
                tr = con.BeginTransaction();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.Transaction = tr;
                cmd.CommandText = "INSERT INTO " + schemaname + ".[WEEKEND]  ([DATE],[DAYNAME]) VALUES ('" + d + "','" + day + "')";
                cmd.ExecuteNonQuery();
                tr.Commit();

            }
        }
    }

找出所有奇数星期六并将它们插入数据库

您可以使用以下LINQ查询:

int year = 2016; // 366 days in 2016 because leap year
DateTime yearStart = new DateTime(year, 1, 1);
int daysInYear = (yearStart.AddYears(1) - yearStart).Days; // 366
var oddSaturdays = Enumerable.Range(0, daysInYear)
    .Select(day => yearStart.AddDays(day))
    .Where(date => date.Day % 2 == 1 && date.DayOfWeek == DayOfWeek.Saturday);
foreach (DateTime oddSaturday in oddSaturdays)
{ 
    // ...
    string sql = string.Format("INSERT INTO {0}.[WEEKEND] ([DATE],[DAYNAME]) VALUES (@DATE, @DAYNAME)", schemaname);
    using (var cmd = new SqlCommand(sql, con))
    {
        cmd.Parameters.Add("@DATE", SqlDbType.Date).Value = oddSaturday;
        cmd.Parameters.Add("@DAYNAME", SqlDbType.VarChar).Value = oddSaturday.DayOfWeek.ToString();
        int inserted = cmd.ExecuteNonQuery();
    }
    // ...
}

对于任何实现IDisposable的对象,始终使用sql参数和using

首先不要在每个for run中打开连接。在for循环之外打开它。

下一步总是关闭连接。

最后知道一个星期天是不是奇数u可以使用模运算符或%

a % b =[剩余]

4 % 2 = 05 % 2 = 1

所以如果u对2取模,u总是知道某个数是奇数,如果结果是1。如果你用datetime。然后对2取模,结果是1这是奇数天。

我的答案是基于Tim的答案,但是找到当月的周数,然后选择奇数:

using System;
using System.Globalization;
int year = 2016; // 366 days in 2016 because leap year
DateTime yearStart = new DateTime(year, 1, 1);
Calendar calendar = CultureInfo.CurrentCulture.Calendar;
int daysInYear = (yearStart.AddYears(1) - yearStart).Days; // 366
var oddSaturdays = Enumerable.Range(0, daysInYear)
    .Select(day => yearStart.AddDays(day))
    .Where(date => date.DayOfWeek == DayOfWeek.Saturday)
    .Select(myDate => 
         new{date=myDate, 
             weekOfMonth = 
            (calendar.GetWeekOfYear(myDate, 
                                   CalendarWeekRule.FirstDay,
                                   DayOfWeek.Sunday)
            ) - 
            (calendar.GetWeekOfYear(new DateTime(myDate.Year, 
                                                 myDate.Month, 1), 
                                    CalendarWeekRule.FirstDay,
                                    DayOfWeek.Sunday)
            ) + 1}
     )
    .Where(myDate => myDate.weekOfMonth % 2 == 1)
    .Select(date => date.date);