我想在多个表中搜索关键字(在asp.net上写的),然后再repeater中显示, 我在网上看到是用union关键字 但我写到我后台就有错 但我5知道是哪里的问题,高手指教啊。下面是我写的代码:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ConnectionString);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from ( select * from zhaoling union select * from main ) where [content] like @content or [title] like @title or [posstime] like @posstime";
        cmd.Connection = con;
        cmd.Parameters.Add(new SqlParameter("@content", "%" + TextBox1.Text + "%"));
        cmd.Parameters.Add(new SqlParameter("@title", "%" + TextBox1.Text + "%"));
        cmd.Parameters.Add(new SqlParameter("@posstime", "%" + TextBox1.Text + "%"));
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        con.Open();
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
弹出的错误是:“/shiwuxitong”应用程序中的服务器错误。
--------------------------------------------------------------------------------在关键字 'where' 附近有语法错误。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.Data.SqlClient.SqlException: 在关键字 'where' 附近有语法错误。源错误: 
行 30:         con.Open();
行 31:         DataSet ds = new DataSet();
行 32:         da.Fill(ds);
行 33:         con.Close();
行 34:         Repeater1.DataSource = ds;
 

解决方案 »

  1.   

    select * from ( select * from zhaoling union select * from main )这个到数据库中执行下看看什么结果。
      

  2.   

    如果再加上where后面的就变成
    where附近有错···
      

  3.   

    如果再加上where后面的就变成 
    where附近有错··· 
      

  4.   

    对MYSQL,微软没有直接支持,所以我用odbc来操作 
    我用的是mysql5的,代码如下: 
    Imports System.Data 
    Imports System.Data.Odbc Partial Class _Default 
      

  5.   

    我用的数据库是sql sever 2000···
      

  6.   

    cmd.CommandText = "select * from ( select * from zhaoling union select * from main ) AS tablename where [content] like @content or [title] like @title or [posstime] like @posstime"; 
      

  7.   

    select * from zhaoling union select * from main --
    因为你两个表数据结构不一样。使用 UNION、INTERSECT 或 EXCEPT 运算符合并的所有查询必须在其目标列表中有相同数目的表达式
      

  8.   

    不知道你哪看来的,两个表select * 居然 union起来了