異なる日付を含む (IDcolumn, int)、(Differencecolumn, int)、および (Datecolumn DateTime) を持つテーブルがあります。
そして、週末を無視して日付の差を計算するメソッド。
public static double GetBusinessDays(DateTime startD, DateTime endD)
{
double calcBusinessDays =
1 + ((endD - startD).TotalDays * 5 -
(startD.DayOfWeek - endD.DayOfWeek) * 2) / 7;
if ((int)endD.DayOfWeek == 6) calcBusinessDays--;
if ((int)startD.DayOfWeek == 0) calcBusinessDays--;
return calcBusinessDays;
}
今日の日付から各 Datecolumn の各 GetBusinessDays 値を取得したいと考えています。そして、それを対応する各 Differencecolumn に挿入します。
例えば
ID Date Difference
1 4-22-2013
2 4-23-2013
3 4-24-2013
今日の日付が 2013 年 4 月 28 日であるとします。差には、それぞれ 6、5、4 が含まれている必要があります。
これは私が今のところやったことですが、うまくいきません:(
myDatabaseConnection.OpenConnection();
mySqlCommand.CommandType = CommandType.Text;
mySqlCommand.CommandText = "select * from Table1 where Difference IS Null";
SqlDataReader sqlreader = mySqlCommand2.ExecuteReader();
int i;
while (sqlreader.Read())
{
i = sqlreader.GetInt32(0);
double y = GetBusinessDays(sqlreader.GetDateTime(1), DateTime.Now);
string commandtext = "Update Table1 SET Difference = " + y + " Where ID = " + i + " ";
mySqlCommand.CommandText = " " + commandtext + " ";
}
myDatabaseConnection.CloseConnection();