如何从我的数据库加载/缓存假期?它将被添加到已经排除周末的DateTimeExtensions中

时间:2013-04-26 14:00:58

标签: c# datetime

public static bool IsHoliday(this DateTime date)
{
  mySqlCommand.CommandType = CommandType.Text;
  mySqlCommand.CommandText = "Select date from HolidayTable”
  SqlDataReader sqlreader = mySqlCommand.ExecuteReader();

  List<DateTime> holidays = new List<DateTime>();  

  while (sqlreader.Read())
  {
      list.Add(reader.GetDateTime(0));
  }    
  holidays.ToArray();
  return holidays.Contains(date.Date);
}

我如何从我的数据库中加载假期?

替换此硬编码。

DateTime[] holidays =
new DateTime[] { 
new DateTime(2013,12,27),
new DateTime(2013,12,28)};

它将被添加到我的DateTimeExtensions,它已经在AddWorkDays()上排除了星期日和星期六

1 个答案:

答案 0 :(得分:0)

public static bool IsHoliday(this DateTime date)
{
ConnectionStringSettings myConnectionString = ConfigurationManager.ConnectionStrings["System.Properties.Settings.ConnectionString"];
SqlConnection mySqlConnection = new SqlConnection(myConnectionString.ConnectionString);
mySqlConnection.Open();
SqlCommand mySqlCommand = new SqlCommand("SELECT Date FROM Holiday", mySqlConnection);
SqlDataReader sqlreader = mySqlCommand.ExecuteReader();

List<DateTime> holidays = new List<DateTime>();

while (sqlreader.Read())
{
holidays.Add(sqlreader.GetDateTime(0));
}
holidays.ToArray();
mySqlConnection.Close();
return holidays.Contains(date.Date);
}

我得到了答案!谢谢:))