0

友達...

私のmsアクセスデータベースでは、日付形式dd-MMM-yyを保存しています。検索クエリでは、日付をパラメーターとして渡します。しかし、私のシステムは日付形式 mm/dd/yyyy を使用できるため、この日付をクエリに渡す前に、この形式を dd-MMM-yy に変換するにはどうすればよいですか。有効な 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);
4

2 に答える 2

2

パラメータを使用します。文字列に変換する必要はありません

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 
    }

}

DateTime選択した値は、日時ピッカー コントロールから直接取得できることに注意してください。DateTimePicker.Valueプロパティの使用

于 2013-08-27T11:03:53.567 に答える