0
數據庫中的DayDate
列的類型爲DateTime
&我在表單中傳遞使用DateTimePicker
制定的字符串。 reader.HasRows
總是返回假!我不知道我做錯了什麼。任何幫助,將不勝感激。我使用的代碼如下。SQL SELECT查詢不從一個簡單的Access數據庫中檢索行
if (!this.con.IsConnected())
{
this.con.Connect();
}
this.cmd = new OleDbCommand("SELECT DayNo FROM [Calendar] WHERE DayDate = " + date + "", this.con.conObj());
this.reader = cmd.ExecuteReader();
this.reader.Read();
int dayNo;
if (this.reader.HasRows)
{
dayNo = int.Parse(reader[0].ToString());
}
else
{
throw new InfoException("The system could not locate the date in the system");
}
你可以發佈'date'的值,你傳入查詢嗎?你也可以更清楚地陳述你試圖達到的目標嗎? – James 2011-12-18 11:23:53
另請注意,您正在調用reader.Read(),然後調用reader.HasRows。要麼調用reader.HasRows首先在封閉if中,要麼使用if(reader.Read()){//做些什麼}。我個人更喜歡if(reader.Read()){},因爲它的代碼行較少。 – dash 2011-12-18 11:26:03
嘗試如果(reader.HasRows && reader.Read()){reader [0] ETC ...} – Lloyd 2011-12-18 11:32:26