为什么此查询字符串在Access中工作而在C#中不工作?
我有一段代码:为什么此查询字符串在Access中工作而在C#中不工作?,c#,sql,ms-access,C#,Sql,Ms Access,我有一段代码: 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); D
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)
因为返回的行可能不止一行。我假设您在这里的行为声明: 当我运行它时,它不会返回任何结果 表示“文本框文本替换为“不匹配”。是这样吗 这将返回许多行 这就解释了。看看你的情况:
if (ds.Tables[0].Rows.Count == 1)
你是说,除非只有一个匹配项,否则没有任何匹配项
您可能想要:
if (ds.Tables[0].Rows.Count > 0)
我假设你在这里的行为陈述: 当我运行它时,它不会返回任何结果 表示“文本框文本替换为“不匹配”。是这样吗 这将返回许多行 这就解释了。看看你的情况:
if (ds.Tables[0].Rows.Count == 1)
你是说,除非只有一个匹配项,否则没有任何匹配项
您可能想要:
if (ds.Tables[0].Rows.Count > 0)
您应该这样做:
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";
}
您应该这样做:
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";
}
是的,我在发帖后就知道了。非常感谢。是的,我在发帖后就知道了。非常感谢。