如果数据表包含多个表,则从数据表中检索数据 [英] Retriving data from data table if it contains more than one table

查看:92
本文介绍了如果数据表包含多个表,则从数据表中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MSsql 2005中创建了一个存储过程,该存储过程包含四个选择语句,用于从四个不同的表中检索数据.现在,我在Web应用程序(带有C#的ASP.NET)中使用存储过程.如果我必须将它们作为数据源传递给四个不同的数据列表,那么如何获取该过程检索到的不同表形式的数据.请提出一些建议,这样我就不必为四个不同的select语句创建四个不同的过程,并且可以轻松地将通过单个过程获取的数据同时绑定到四个不同的数据列表.
谢谢


存储过程是:

i hv made a stored procedure in MSsql 2005 which contains four select statments for retriving data from four different tables. now i m using the stored procedure in my web application (asp.net with c#). how can i get access to data in the form of different tables retrived by the procedure if i have to pass them as datasource to four different datalists. Please Suggest something ,so that i have not to made four different procedures for four different select statements and the data fetched by single procedure can be easily bind to four different datalists simultaneously.
Thanx


stored procedure is:

ALTER PROCEDURE [dbo].[sp_select_interest]
    -- Add the parameters for the stored procedure here
    ( @uid int
)
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    -- Insert statements for procedure here
    select music_liked.uid,music_liked.music_id,music.music_pic from music_liked,music where music_liked.music_id = music.music_id and music_liked.uid = @uid
    select movies_liked.uid,movies_liked.movies_id,movies.movies_pic from movies_liked,movies where movies_liked.movies_id = movies.movies_id and movies_liked.uid = @uid
    select books_liked.uid,books_liked.books_id,books.books_pic from books_liked,books where books_liked.books_id = books.books_id and books_liked.uid = @uid
    select games_liked.uid, games_liked.games_id, games.game from games_liked,games where games_liked.games_id = games.games_id and games_liked.uid = @uid
END

推荐答案

从此处开始:
Start here: Populate a DataSet with multiple DataTable objects using multiple SELECT statements[^]: not great but it''ll get you going.

/>

ALTER PROCEDURE [dbo].[sp_select_interest]
    -- Add the parameters for the stored procedure here
    ( @uid int,
      @Type varchar

)
AS
BEGIN
    
if @Type = 'Music'
begin
    select music_liked.uid,music_liked.music_id,music.music_pic from music_liked,music where music_liked.music_id = music.music_id and music_liked.uid = @uid
end
If @Type = 'Movies'
begin
    select movies_liked.uid,movies_liked.movies_id,movies.movies_pic from movies_liked,movies where movies_liked.movies_id = movies.movies_id and movies_liked.uid = @uid
end

if @Type = 'Books'
begin
    select books_liked.uid,books_liked.books_id,books.books_pic from books_liked,books where books_liked.books_id = books.books_id and books_liked.uid = @uid
end
.  
.
END


这篇关于如果数据表包含多个表,则从数据表中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆