我有两个表,是table1,table2
table1中有字段id,name
table2 id ,idname,name
idname =id
GridView1控件调出table2中的内容,且idname显示name 字段的内容,怎么做?
table1中有字段id,name
table2 id ,idname,name
idname =id
GridView1控件调出table2中的内容,且idname显示name 字段的内容,怎么做?
查询的时候select table2.id, table1.name as t1name, table2.name as t2name
from table1 inner join table2
on table1.id = table2.idname
写个链接查询
select a.id,a.name,b.name as bname from talbe1 b,table2 a where a.idname=b.id
这只是其中之一方法很多 sql视图...都可以
from
table1
inner join table2 on table1.id = table2.idname
go
use demo
go
create table table1
(
id int identity primary key,
name varchar(10)
)
go
create table table2
(
id int identity primary key,
idname int references table1(id),
name varchar(10)
)
go
insert into table1 values('aa')
insert into table1 values('bb')insert into table2 values(1, 'XXX')
insert into table2 values(1, 'XXXXXX')
insert into table2 values(2, 'YYY')select table2.id, table1.name as t1name, table2.name as t2name
from table1 inner join table2
on table1.id = table2.idname<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True"
SortExpression="id" />
<asp:BoundField DataField="t1name" HeaderText="t1name" SortExpression="t1name" />
<asp:BoundField DataField="t2name" HeaderText="t2name" SortExpression="t2name" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\sqlexpress;Initial Catalog=Demo;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="select table2.id, table1.name as t1name, table2.name as t2name from table1 inner join table2 on table1.id = table2.idname"></asp:SqlDataSource>
</form>
</body>
</html>
GridView1控件调出数据说找不到,字段