在Access2010中选择Query Works正常但在C#代码中没有

时间:2015-12-08 10:31:20

标签: c# sql ms-access ms-access-2010

我有一个简单的Select Query,它在Access中运行良好,但在C#代码中运行不正常 这是查询

SELECT FIRST(ItemName) As Item 
from SubmitData  
where ItemName like 'txt_rdo_breaks*' 
and Deleted=false

这是Access中的结果 enter image description here

但是在C#代码中它什么都没有 这是C#代码

 cmd1.CommandText = "SELECT FIRST(ItemName) As Item from SubmitData  where ItemName like 'txt_" + controlPatrs[0] + "*' and Deleted=false";
 OleDbDataReader reader1 = cmd1.ExecuteReader(System.Data.CommandBehavior.Default);
 while (reader1.Read())
 {
     if (Convert.ToString(reader1["Item"]).Length > 1)
     {
          tag += Convert.ToString(reader1["Item"]) + "|txt_" + controlPatrs[0] + "|" + controlPatrs[2] + "||";
     }

  }
 reader1.Close();

enter image description here

1 个答案:

答案 0 :(得分:0)

关注 Ivan Stoev的评论

OleDbProvider query seems to require a different wildcard characters - '%'

在Access中使用*符号,但使用OleDbProvider %符号时有效。 所以正确的命令文本是

cmd1.CommandText = "SELECT FIRST(ItemName) As Item from SubmitData  where ItemName like 'txt_" + controlPatrs[0] + "%' and Deleted=false";