我有這段代碼:爲什麼這個查詢字符串在Access而不是C#中工作?
conn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=c:\\myDB.accdb");
conn.Open();
sql = string.Format("SELECT Version FROM tblVersions where [FUL Flag] = 'Y'");
OleDbDataAdapter da = new OleDbDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds);
dt = ds.Tables[0];
if (ds.Tables[0].Rows.Count == 1)
{
tV.Text = dt.Rows[0][0].ToString(); //only looking for the first result
}
else
{
tV.Text = "no match";
}
當我運行它,它不返回任何結果。但是,如果我複製SELECT
語句並將其右側粘貼到Access中的查詢窗口中,它確實會查找結果。這裏是我粘貼到Access:
SELECT Version FROM tblVersions where [FUL Flag] = 'Y'
這會返回很多行。
我錯過了某處的區別嗎?謝謝!
編輯:找到了解決辦法。我應該尋找
(ds.Tables[0].Rows.Count > 0)
,而不是
(ds.Tables[0].Rows.Count == 1)
由於超過1行可退還。
是啊,我抓住了這張貼我的問題之後。謝謝! – 2013-03-11 18:14:01