2

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?

1
  • Did you try with UNION in one Sqldatasource ? see my answer below. You could of course still use WHERE-clause when using unions. E.g. select... where id = xx... Commented Sep 14, 2013 at 10:03

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

1+ , The UNION operator selects only distinct values by default. To allow duplicate values, use the ALL keyword with UNION
1

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.

Comments

0

Go here: Union

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.