我在实体集合的存储过程中的第一栏ForumGroup遇到了问题。我得到这个错误。
尝试从列序号“ 0”读取是无效的。使用CommandBehavior.SequentialAccess,您只能从列序号“ 3”或更高版本中读取。
我的SQL:
DECLARE @Forums TABLE(ForumGroup nvarchar(100), Title varchar(100), Description varchar(100), ThreadCount int, LastPostBy nvarchar(50),
LastPostDate datetime, LastPostTitle varchar(100))
INSERT INTO @Forums(ForumGroup, Title, Description, ThreadCount, LastPostBy, LastPostDate, LastPostTitle)
SELECT
(
CASE WHEN F.ParentID IS NOT NULL THEN
(SELECT Title FROM Forums S WHERE S.ForumID = F.ParentID)
ELSE
(SELECT Title FROM Forums S WHERE S.ParentID IS NULL)
END) AS ForumGroup,
Title, Description,
ThreadCount = (SELECT COUNT(*) FROM Posts P WHERE P.ForumID = F.ForumID),
LastPostBy = (SELECT TOP 1 AddedBy FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC),
LastPostDate = (SELECT TOP 1 AddedDate FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC),
LastPostTitle = (SELECT TOP 1 Title FROM Posts P WHERE P.ForumID = F.ForumID ORDER BY P.PostID DESC)
FROM Forums F WHERE ParentID IS NOT NULL
ORDER BY ForumGroup
SELECT * FROM @Forums
我的C#:
public class Forums
{
public List<Forum> GetForums()
{
using (EntityConnection conn = new EntityConnection("name=CMSEntities"))
{
conn.Open();
EntityCommand cmd = conn.CreateCommand();
cmd.CommandText = "CMSEntities.sproc_Forums_GetForums";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
using (EntityDataReader reader = cmd.ExecuteReader(System.Data.CommandBehavior.SequentialAccess))
{
List<Forum> forums = new List<Forum>();
while (reader.Read())
{
Forum forum = new Forum(
1,
"",
DateTime.Now,
reader["Title"].ToString(),
reader["Description"].ToString(),
0,
false,
null,
null,
null,
true,
reader["ForumGroup"].ToString(),
(int)reader["ThreadCount"],
null,
DateTime.Now,
null);
forums.Add(forum);
}
return forums;
}
}
}
}
最佳答案
看看这个blog post.。
引用博客文章:
如果使用SequentialAccess打开阅读器,则必须阅读以下各列
依次,您无法阅读第1列,然后阅读第0列。
反过来做。