当前位置:编程学习 > C#/ASP.NET >>

关于SQL语句查询的问题【C#】

  protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox3.Text != "")
        {
            string sSql = "select * from Goods where title like '%" + TextBox3.Text + "%'and actor like '%" + TextBox3.Text + "%'";
            SQL s = new SQL();
            DataSet ds = s.DSSearch(sSql);// s.DSSearch(sSql);
            PagedDataSource pds = new PagedDataSource();//初始化PagedDataSource实例,主要用做DataList1的分页使用
            pds.AllowPaging = true;//允许分页
            pds.PageSize = 15;//每页显示10条
            pds.DataSource = ds.Tables[0].DefaultView;//将检索出的ds数据集绑定给PagedDataSource实例pds

            DataList2.DataSource = pds;//将pds内容最终绑定给DataList1,呈现到界面中
            DataList2.DataBind();
        }
    }

红字部分写了title和actor两个关键字搜索,可是运行的时候两个关键字都查询不到。如果只写一个title或者actor就可以查询出来。。。没有显示代码错误。 SQL C# 搜索 --------------------编程问答-------------------- 调试的时候可以将sql语句取出来 单独执行一下 很容易看出问题的
目测and前面应该留空格 可以试一下 --------------------编程问答--------------------  注意空格
string sSql = "select * from Goods where title like '%" + TextBox3.Text + "%' and actor like '%" + TextBox3.Text + "%'"; --------------------编程问答-------------------- string sSql = "select * from Goods where (title like '%" + TextBox3.Text + "%' ) or (actor like '%" + TextBox3.Text + "%')";
你换用 or 试试 --------------------编程问答-------------------- 你用的 and 肯定找不到啦  要用 or  楼上正解啦 --------------------编程问答--------------------
引用 2 楼 Chinajiyong 的回复:
 注意空格
string sSql = "select * from Goods where title like '%" + TextBox3.Text + "%' and actor like '%" + TextBox3.Text + "%'";


同意2l应该是空格问题吧。 and 前面应该有个空格吧 --------------------编程问答-------------------- 跟空格没关系(SQL Server 2005测了)。
感觉是没有同时满足两个条件的数据。
建议楼主做个测试,先确保有同时满足两个条件的数据,再测测这段SQL是否能查出数据。 --------------------编程问答--------------------
引用 3 楼 zhangxiangsang 的回复:
string sSql = "select * from Goods where (title like '%" + TextBox3.Text + "%' ) or (actor like '%" + TextBox3.Text + "%')";
你换用 or 试试


改了,可以了。谢谢~~我来看看怎么给分…… --------------------编程问答--------------------
引用 3 楼 zhangxiangsang 的回复:
string sSql = "select * from Goods where (title like '%" + TextBox3.Text + "%' ) or (actor like '%" + TextBox3.Text + "%')";
你换用 or 试试


貌似已经给过分了哈~~~
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,