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;
}

解决方案 »

  1.   

    2是从acess查询里直接查询到的,以前做的都是sql,acess很少用,大家帮忙看看。
      

  2.   

    可能是因爲CommandBehavior.CloseConnection,
    OleDbDataReader dr=cmd.ExecuteReader();試試
      

  3.   

    strig Query="select count(id) as re from news where title like '%"+keyword+"%' or content like '%"+keyword+"%'";
      

  4.   


    smile9961(good life) 去了,一样没有结果killau(大柄) acess和sql的语法不一样,你的是不正确的哦。
      

  5.   

    确定了是模糊查询的问题,改成select count(id) from news 就能得到结果。
    加上like就是0了,,郁闷,怎么办???
      

  6.   

    你跟蹤一下看看執行的是哪條語句,會不會是執行了這句:
    sql="select count(id) as re from news where (title like '*"+keyword+"*' or content like '*"+keyword+"*') and news.sortid="+sort;
    而結果為0?我覺得是因爲執行結果本身就是0而不是2。
      

  7.   

    如果是access环境中,like用*号,但在C#中应该还是用%号吧?
      

  8.   

    不是的,语句没有问题。抛开这个
    我新建了一个页面:
    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);
    OleDbCommand cmd=new OleDbCommand("select count(id) from news where title like '*1*'",conn);//////这句改成select count(id) from news 就能得到结果
    conn.Open();
    OleDbDataReader dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
    dr.Read();
    int re=Convert.ToInt32(dr[0].ToString());
    dr.Close();
    conn.Close();
    Response.Write(re.ToString());在看微软的知识库,看看有没有答案。  有做过类似搜索的兄弟,麻烦告知,谢谢!!!
      

  9.   

    vbman2003(家人)   谢谢,改过了,好了。
      

  10.   

    确实在c#中查询是%号, 难道C#会编译sql语句?  跟踪了一下,发现sql语句确实是select count(id) from news where title like '%1%',但是这句在acess查询里查询不到结果。这中间的转换是谁在捣鬼???  难道是oledb??
      

  11.   

    这个也原因也不是很清楚,据说是jet-sql的二种模式ANSI-89 SQL和ANSI-92 SQL造成的,可能是因为你连接access的数据Microsoft.Jet.OLEDB.4.0支持的是ANSI-92 SQL模式吧?
      

  12.   

    在jet-sql手册中有这么一句:仅当使用 Microsoft® Jet 4.X 版和 Microsoft OLE DB Provider for Jet 时,ANSI SQL 通配符 (%) 和 (_) 才可用。若使用 Microsoft Access 或 DAO,则将其视为原义字符。