2013-08-27 2 views
0

친구 ... 내 MS 액세스 데이터베이스에구문 분석 날짜는

은 내가 날짜 형식 DD-MMM-YY를 저장합니다. 및 검색어에서 매개 변수로 날짜를 전달합니다. 하지만 내 시스템은 날짜 형식 mm/dd/yyyy 입니다. dd-MMM-yy에서이 형식을 변환하려면 어떻게해야합니까? 쿼리에이 날짜를 전달하기 전에 지금 folling code ..but 오류를 사용합니다 .... String 유효한 DateTime으로 인식되지 않았습니다.

DateTime dt = DateTime.ParseExact(startdate.ToString(), "dd-MMM-yy", 
            CultureInfo.InvariantCulture);//startdate is datepicker 

쿼리 ...

s = new OleDbDataAdapter(" 
     SELECT opd_patient_master.*, patient_operation.* 
     FROM opd_patient_master, patient_operation 
     WHERE opd_patient_master.pid= patient_operation.pid 
     and opd_patient_master.rdid= patient_operation.rdid 
     and odate >= #" + startdate + "# and odate<=# " + enddate + "# 
     and operation= '" + oprtype + "'", mycon); 
+0

와'enddate' 현재의 접근 방식에 대한

는, 해결책은 문자열로 날짜를 통과, 표준 ISO 날짜 포맷 YYYY-MM-DD를 사용하는 것입니다 또한 'DateTimePicker'입니까? –

답변

2

사용 매개 변수, 당신은 당신이 날짜 시간 선택기에서 선택한 DateTime 값을 얻을 수 있습니다

using (var cmd = new OleDbCommand("SELECT opd_patient_master.*, patient_operation.* FROM opd_patient_master, patient_operation where opd_patient_master.pid= patient_operation.pid and opd_patient_master.rdid= patient_operation.rdid and odate >= ? and odate<= ? and operation= ?", mycon)) 
{ 
    cmd.Parameters.AddWithValue("@startdate", startDateTimePicker.Value); // use DateTime input here as startdate 
    cmd.Parameters.AddWithValue("@enddate", endDateTimePicker.Value); // use DateTime input here as enddate 
    cmd.Parameters.AddWithValue("@oprtype", oprtype); 
    using (OleDbDataAdapter s = new OleDbDataAdapter(cmd)) 
    { 
     // do something with adapter 
    } 

} 

주를 문자열로 변환 할 필요가 없습니다 직접 제어. DateTimePicker.Value 속성을 사용하는 것

+1

'OP '가 DateTimePicker에서 DateTime 값을 추출하는 방법을 모르는 것 같아요. (DateTimePicker.ToString()을 사용하여 DateTime 값을 얻었습니다.) –

+0

@KingKing good point Answer – Damith

+0

'startdate.ToString()'은 명확히하기 위해 DTP 컨트롤의 문자열 표현을 반환하지만 부가 적으로 필요하지는 않을 수도 있습니다. –

0

parameter queries를 사용하는 것이 훨씬 더 좋은 방법입니다. 그런 다음이 포맷 문제는 처리됩니다.

string sStartDate = startdate.Value.ToString("yyyy-MM-dd"); 

".. and odate >= #" + sStartDate + "#.. 

Custom Date and Time Formats : MSDN