A B C D E五个表,name 为每个表都有的项,表示相同内容,且都是主键,每个表都是对于name有唯一记录。
现在要根据查询条件,例如c.mm="小王' and d.time="2006-3-3",联查五个表中的任意几项,并且动态绑定到一个datagrid上,查询条件和需要显示的项都是动态的,写在Label中。
请大家帮忙,告诉我如何实现,代码怎么写,我在网上找了些资料,可是加到我的程序里总是报错,解决不了。
先谢谢了

解决方案 »

  1.   

    select a.字段名,a.字段名,b.字段名  from a  join b on a.id=b.id
      join c on c.id=a.id
      

  2.   

    Label1.Text=Request.QueryString["var1"];//选项
    Label2.Text=Request.QueryString["var2"];//查询条件
    string strConnection=System.Configuration .ConfigurationSettings.AppSettings ["Connect"];
    SqlConnection con=new SqlConnection (strConnection);
    SqlDataAdapter sda=new SqlDataAdapter();
    SqlCommand sc=con.CreateCommand();
    sda.SelectCommand=sc;
    //DataTable newtable = ds1.Tables.Add("newtable");
    con.Open();
    if(Label2.Text!="")
    sc.CommandText="SELECT "+Label1.Text+" FROM A INNER JOIN B ON A.name = B.name INNER JOIN C ON A.name = C.name INNER JOIN E ON A.name = E.name INNER JOIN D ON A.name = D.name where "+Label2.Text;
    else
    sc.CommandText="SELECT "+Label1.Text+" FROM A INNER JOIN B ON A.name = B.name INNER JOIN C ON A.name = C.name INNER JOIN E ON A.name = E.name INNER JOIN D ON A.name = D.name";
    DataGrid1.DataSource=ds1.Tables["newtable"];
    DataGrid1.DataMember=("newtable");
    sda.Fill(ds1);
    this.DataGrid1.DataBind();
    con.Close();总是报错,郁闷阿
      

  3.   

    Label1.text="A.mm,B.nn,C.ii,D.pp"
    Label2.text="A.ss=oo or B.ww=qq"
      

  4.   

    你把你的SQL截取出来放在查询分析器里面执行看是否能正常执行分清楚是你的SQL问题还是代码问题
      

  5.   

    Label1.Text=Request.QueryString["var1"];//选项
    Label2.Text=Request.QueryString["var2"];//cx
    string strConnection=System.Configuration .ConfigurationSettings.AppSettings ["Connect"];
    SqlConnection con=new SqlConnection (strConnection);
    SqlDataAdapter sda=new SqlDataAdapter();
    SqlCommand sc=con.CreateCommand();
    con.Open();
    if(Label2.Text!="")
    sc.CommandText="SELECT "+Label1.Text+" FROM (codecharacter JOIN codeintro ON codecharacter.codename = codeintro.codename JOIN messagecharacter ON codecharacter.codename = messagecharacter.codename JOIN research ON codecharacter.codename = research.codename JOIN spy_control ON codecharacter.codename = spy_control.codename) where "+Label2.Text;
    else
    sc.CommandText="SELECT "+Label1.Text+" FROM (codecharacter JOIN codeintro ON codecharacter.codename = codeintro.codename JOIN messagecharacter ON codecharacter.codename = messagecharacter.codename JOIN research ON codecharacter.codename = research.codename JOIN spy_control ON codecharacter.codename = spy_control.codename)";
    DataGrid1.DataSource=ds1.Tables["codecharacter"];
    DataGrid1.DataMember=("codecharacter");
    sda.SelectCommand=sc;
    sda.Fill(ds1,"codecharacter");
    this.DataGrid1.DataBind();
    con.Close();
    调整了一下,现在抱错:
    未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
      

  6.   

    打断点..把最终要执行的sql取出来..在查询分析器中试一下..看是不是sql有问题
      

  7.   

    DataGrid1.DataSource=ds1.Tables["newtable"];
    DataGrid1.DataMember=("newtable");
    sda.Fill(ds1);
    是不是顺序搞错了哟?
      

  8.   

    随着程序小得改动,会报不同的错,有时是“FORM"附近有语法错,有时是“DATAGRID不能创建任何列“,反正就是改不出来,怎么改怎么报错:(
      

  9.   

    把最终产生的sql贴出来..sda.Fill(ds1);
    DataGrid1.DataSource=ds1.Tables["newtable"].DefaultView;
    this.DataGrid1.DataBind();
      

  10.   

    lovehongyun的方法,搞定了
    太好了,非常感谢