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;
}
得到结果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;
}
解决方案 »
- 用过ASP.NET模板的麻烦进来一下
- 2005网站访问问题!!在线等,急!!!
- 在devexpress的aspxgridview中或者就是gridveiw自带"新建"一行,如何在这个新建行里实现联动!急
- 这个问题太困扰我了,请求帮助!
- 服务器是win2003,asp.net 程序 调用服务器上web service时,出现404错是什么原因,本地调过,是正确的
- 控件datagrid中的一个列为数据库表中的字符串型字段str1,要求显示str2=fun(str1)到datagrid列上,如何做?fun(str1)是对str1做处理的一
- .NET调用eWebEditor编辑器的问题。。。
- 不用redirect,新开一个窗口的问题。
- 单元格中是label,我怎么去找到它呀???
- 如何统计用vs2005开发的网站的源代码总行数是多少?急急急!
- TreeView 中的问题(转贴)
- BinaryWrite输入文件内容乱码
OleDbDataReader dr=cmd.ExecuteReader();試試
smile9961(good life) 去了,一样没有结果killau(大柄) acess和sql的语法不一样,你的是不正确的哦。
加上like就是0了,,郁闷,怎么办???
sql="select count(id) as re from news where (title like '*"+keyword+"*' or content like '*"+keyword+"*') and news.sortid="+sort;
而結果為0?我覺得是因爲執行結果本身就是0而不是2。
我新建了一个页面:
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());在看微软的知识库,看看有没有答案。 有做过类似搜索的兄弟,麻烦告知,谢谢!!!