c# - 尝试从列序号 '0' 读取无效

标签 c# asp.net sql entity-framework

我在实体集合的存储过程中的第一列 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. .

引用博文:

If you open a reader with SequentialAccess you must read the columns sequentially, you can't read column 1 and then read column 0. You have to do it the other way around.

关于c# - 尝试从列序号 '0' 读取无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155040/

相关文章:

c# - 每个数据库行的 NUnit 测试?

c# - 从 RESTful WCF 服务返回图像

asp.net - IE 地址栏和源代码中出现奇怪的字符串

asp.net - GridView 没有任何属性或属性来生成列

sql - 如何在oracle数据库中存储枚举字段类型的对象

c# - 向列表元素添加引号

c# - 如何在 Windows 8 中安装 XNA Game Studio 4.0?

c# - 如何从 ServiceStack 查询返回图像?

一行一对多的mysql

SQL 字符串连接,仅当 column 不为 null 时才以 ',' 分隔