0

我有这段代码:

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 行。

4

2 回答 2

3

I'm assuming that your statement of behaviour here:

When I run it, it doesn't return any results.

means "the TextBox text is replaced with 'no match'". Is that right?

This returns many rows.

Well that explains it. Look at your condition:

if (ds.Tables[0].Rows.Count == 1)

You're claiming there are no matches in every case unless there's exactly one match.

You probably want:

if (ds.Tables[0].Rows.Count > 0)
于 2013-03-11T18:13:32.987 回答
1

你应该做这个:

ds.Tables[0].Rows.Count > 0 instead of ==1

完整示例:

if (ds.Tables[0].Rows.Count > 0)
{
    tV.Text = dt.Rows[0][0].ToString();    //only looking for the first result
}
else
{
    tV.Text = "no match";
}
于 2013-03-11T18:19:24.370 回答