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

时间:2015-09-03 07:28:03

标签: c# sql-server

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

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


            }
        }
    }

3 个答案:

答案 0 :(得分:5)

您可以使用以下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();
    }
    // ...
}

对于实现using的任何对象,也总是使用sql参数和IDisposable

答案 1 :(得分:1)

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

接下来总是关闭连接。

最后要知道周日是否奇怪你可以使用模运算符或%

a%b = [剩余]

4%2 = 0 5%2 = 1

因此,如果你对2进行模数运算,如果结果为1,你将总是知道某些事情是奇数。所以如果你采用datetime.day并将其模数为2,结果为1则是奇数日。

答案 2 :(得分:0)

我的回答是基于蒂姆的回答,但是找到了当月的周数,然后选择奇数:

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