A B C D E五个表,name 为每个表都有的项,表示相同内容,且都是主键,每个表都是对于name有唯一记录。
现在要根据查询条件,例如c.mm="小王' and d.time="2006-3-3",联查五个表中的任意几项,并且动态绑定到一个datagrid上,查询条件和需要显示的项都是动态的,写在Label中。
请大家帮忙,告诉我如何实现,代码怎么写,我在网上找了些资料,可是加到我的程序里总是报错,解决不了。
先谢谢了
现在要根据查询条件,例如c.mm="小王' and d.time="2006-3-3",联查五个表中的任意几项,并且动态绑定到一个datagrid上,查询条件和需要显示的项都是动态的,写在Label中。
请大家帮忙,告诉我如何实现,代码怎么写,我在网上找了些资料,可是加到我的程序里总是报错,解决不了。
先谢谢了
join c on c.id=a.id
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();总是报错,郁闷阿
Label2.text="A.ss=oo or B.ww=qq"
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();
调整了一下,现在抱错:
未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
DataGrid1.DataMember=("newtable");
sda.Fill(ds1);
是不是顺序搞错了哟?
DataGrid1.DataSource=ds1.Tables["newtable"].DefaultView;
this.DataGrid1.DataBind();
太好了,非常感谢