2013-12-11 2 views
0

SQL 쿼리에서 작동하는 매개 변수로 DateTime을 사용하는 데 문제가 있습니다. 여기 C# DateTime MySQL 명령

내가하고자하는 쿼리입니다 :

GAME_TIME는 DateTime 개체이며 Gameoutcome.gameDate는 DATETIME 데이터 타입이다
command.CommandText = "SELECT idGameNumber FROM GameOutcome WHERE GameOutcome.gameDate = <insert DateTime game_time>"; 

.

WHERE 절에 = 기호의 오른쪽에 무엇을 넣어야합니까?

답변

4

해결 방법 1 : 사용 DateTime 문자열

MYSQL은 기본 형식 다음에 DateTime를 취합니다

yyyy-MM-dd HH:mm:ss

그래서 당신은 위의 format으로 datetime 개체를 변환 할 수 있습니다.

이 시도 :

command.CommandText ="SELECT idGameNumber FROM GameOutcome WHERE GameOutcome.gameDate ='"+ game_time.ToString("yyyy-MM-dd HH:mm:ss")+"'"; 

해결 방법 2 :parameterised queries를 사용하여.

당신은 이미 parameterised queries에 대해 들었을 수도 있습니다.
매개 변수화 된 쿼리는 sql injection attacks을 피할뿐만 아니라 테이블에서 feilds에 arguments을 전송/전달하는 명확한 방법을 제공합니다.

command.CommandText ="SELECT idGameNumber FROM GameOutcome WHERE GameOutcome.gameDate [email protected];"  
command.Parameters.AddWithValue("@gamedate",game_time);