다른 날짜를 포함하는 (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();
좋습니다. 시도해 보겠습니다. databaseConnection을 닫지 않는 것이 괜찮습니까? –
데이터베이스 연결 개체가 IDisposable 인터페이스 (SqlConnection 않습니다)를 구현하는 한 실행이'using' 블록의 끝 (이 경우에는 가장 바깥 쪽 사용 블록)에 도달하면 닫힙니다. –
오류 "이미 닫혀 있어야하는이 명령과 연결된 열린 DataReader가 이미 있습니다". executenotquery 전에 sqlreader.close를 추가합니다. 단 하나의 행이 업데이트되고 "리더가 닫히면 Read를 호출하는 동안 잘못된 시도가 발생했습니다." –