Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

You can also create a view in your database that will collect all the data you need and then query it in your c# application.

Go here: Union

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top