I've 3 tables: users
, feed(id,body,title,link)
and userstofeed(id,userid,feedid,isread)
while i'm trying to read bool value from userstofeed table it bring IndexOutOfRangeException
. Tell me please what I'm doing wrong
while (reader.Read())
{
rssFeed.Title = reader["title"].ToString();
rssFeed.Body = reader["body"].ToString();
rssFeed.Link = reader["link"].ToString();
rssFeed.IsRead = (bool) reader["isread"]; //IndexOutOfRangeException here
yield return rssFeed;
}
And here sql store procedure:
create proc spGetItemsByUser
@userName nvarchar(50)
as
begin
declare @userId int
declare @feedId table (id int)
select @userId = id
from Users
where name = @userName
insert into @feedid (id)
select feedid, isread
from userstofeed
where userid = @userId
select * from feed where id in (select id from @feedId)
select isread from userstofeed where userid = @userId //Here I'm getting bit if it read or not
end
Change the SELECT
s in your stored procedure to this:
SELECT [feed].[title], [feed].[body], [feed].[link], [userstofeed].[isread]
FROM [feed]
INNER JOIN [userstofeed] ON ([userstofeed].userid = @userId)
WHERE [feed].[id] in (select id from @feedId)
I think in your version isread
is not a column of the first result set. The isread
values are appended as rows after the rows for title
, feed
and body
.
As Zohar Peled suggested, I try to give a refactoring for the whole stored procedure:
CREATE PROC spGetItemsByUser
@userName nvarchar(50)
AS
BEGIN
SELECT [feed].[title], [feed].[body], [feed].[link], [userstofeed].[isread]
FROM [feed]
INNER JOIN [userstofeed] ON ([userstofeed].[feedid] = [feed].[id])
INNER JOIN [users] ON ([users].[id] = [userstofeed].[userid])
WHERE [users].[name] = @userName
END
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.