简体   繁体   中英

How can i select from more than one table in a SqlDataSource asp.net

i have a web page, that will load data from three tables is there a way to load all the data at once using a Sqldatasource, or must i do it in coding?

i also want to sort it. but that i know how to do. and all the data goes into a gridview, which is why i want to use just one Sqldatasource.

this is what i have so far:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ 
ConnectionStrings:testpicsConnectionString %>" SelectCommand="SELECT * FROM [tball]
select * from tb1
select * from tb2"></asp:SqlDataSource>

and do i just add

sort id desc

to them all?

i just want to know can it be done with one Sqldatasource?

btw, I'm using c# and sql (obviously)

sorry forgot to add the schema from the tables

all the tables are the same: they have id, desc and pic

id int primary key identity not null,
[desc] varchar(max) null,
pic varbinary(max) null

that is used for all tables. so if i had to join, on where or what would i join?

you could use UNION to select the columns from the different tables...

select * from tb1
union all 
select * from tb2
union all
select * from tb3
order by id desc;

union all - will show all records in your result.
union - will remove duplicate records from your result.

Union all will give better performance as the database engine won't have any overhead for managing the result to remove the duplicates.

您还可以在数据库中创建一个视图,该视图将收集所需的所有数据,然后在c#应用程序中对其进行查询。

Go here: Union

select * from Table1 union all select * from Table2 order by 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM