读取Acess数据库,奇怪问题。
sql= "select count(id) from news where (title like '* "+keyword+ "* ' or content like '* "+keyword+ "* ') ";
这句断点以后得到sql= "select count(id) from news where title like '*1* ' or content like '*1* ') ";
得到结果2
可是用OleDbDataReader无法得到结果,换了语句也不行,以下是代码:
private static int getcount()
{
string constr= "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= "+System.Web.HttpContext.Current.Server.MapPath(System.Configuration.ConfigurationSettings.AppSettings[ "cs "])+ "; ";
OleDbConnection conn=new OleDbConnection(constr);
string sql;
if(sort== "0 ")
{
sql= "select count(id) as re from news where (title like '* "+keyword+ "* ' or content like '* "+keyword+ "* ') ";
}
else
{
sql= "select count(id) as re from news where (title like '* "+keyword+ "* ' or content like '* "+keyword+ "* ') and news.sortid= "+sort;
}
OleDbCommand cmd=new OleDbCommand(sql,conn);
conn.Open();
OleDbDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
int re=Convert.ToInt32(dr[ "re "].ToString());///这个地方得不到2这个值,总是0
dr.Close();
return re;
}
conn.Close();
return 0;
}
[解决办法]
可能是因爲CommandBehavior.CloseConnection,
OleDbDataReader dr=cmd.ExecuteReader();試試
[解决办法]
strig Query= "select count(id) as re from news where title like '% "+keyword+ "% ' or content like '% "+keyword+ "% ' ";
[解决办法]
如果是access环境中,like用*号,但在C#中应该还是用%号吧?