InvalidOperationException:不存在数据时尝试读取无效。 (SQL)

发布于 2024-11-17 01:16:18 字数 1524 浏览 0 评论 0原文

      SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString);

    SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
    cmd.Parameters.Add("@ThreadsID", SqlDbType.Int).Value = commentIDe;
    cmd.Parameters.Add("@CommentsID", SqlDbType.Int).Value = commentIDe;
    try
    {
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dr != null && dr["Comments"] != null && dr["Name"] != null && dr["Date"] != null && dr["UserID"]!=null)//> Invalid attempt to read when no data is present.
        {
            Comment = dr["Comments"].ToString();
            UserName = dr["Name"].ToString();
            Reputation=Int32.Parse(dr["Reputation"].ToString());
            Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
            UserID = (Guid)dr["UserID"];
        }
        dr.Close();
    }
    finally
    {
        if (conn.State != ConnectionState.Closed)
        {
            conn.Close();
        }
    }

注意:我确实使用 while(dr.Read){} 迭代了那段代码...此处未显示。

为什么我会收到该异常以及如何摆脱它

更新:

                 while (reader.Read())//Command runs through all the ThreadIDs that i have!
                {
                    Comments allQ = new Comments((int)reader["CommentsID"]);
                    allComments.Add(allQ);
                }

注释是代码所在的类,并且它在构造函数内有一个方法来运行我提供的代码。

可能是如果循环运行太多次..那么就会抛出异常,对吗?

      SqlConnection conn = new SqlConnection(AllQuestionsPresented.connectionString);

    SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
    cmd.Parameters.Add("@ThreadsID", SqlDbType.Int).Value = commentIDe;
    cmd.Parameters.Add("@CommentsID", SqlDbType.Int).Value = commentIDe;
    try
    {
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

        if (dr != null && dr["Comments"] != null && dr["Name"] != null && dr["Date"] != null && dr["UserID"]!=null)//> Invalid attempt to read when no data is present.
        {
            Comment = dr["Comments"].ToString();
            UserName = dr["Name"].ToString();
            Reputation=Int32.Parse(dr["Reputation"].ToString());
            Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
            UserID = (Guid)dr["UserID"];
        }
        dr.Close();
    }
    finally
    {
        if (conn.State != ConnectionState.Closed)
        {
            conn.Close();
        }
    }

Note: I do iterate over that piece of code with while(dr.Read){}... it isnt shown here.

Why do i get that exception and how do i get rid of it

UPDATE:

                 while (reader.Read())//Command runs through all the ThreadIDs that i have!
                {
                    Comments allQ = new Comments((int)reader["CommentsID"]);
                    allComments.Add(allQ);
                }

Comments is the class that the code is in, and it has a method inside the constructor that runs the code that i presented.

It could be that if the loop runs too many times.. Then the exception is being thrown am i right?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-11-24 01:16:18

您的代码的以下部分是非法的

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr != null && dr["Comments"] ...)

您必须在访问 SqlDataReader 的索引器之前调用一次 Read 方法(并验证它是否返回 true),如文档所述:

SqlDataReader 的默认位置是在第一条记录之前。因此,您必须调用 Read 才能开始访问任何数据。

The following part of your code is illegal

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

if (dr != null && dr["Comments"] ...)

You have to call the Read method once (and verify that it returns true) before accessing the indexer of the SqlDataReader, as the documentation states:

The default position of the SqlDataReader is before the first record. Therefore, you must call Read to begin accessing any data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文