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

发布于 2024-11-14 20:45:26 字数 1263 浏览 2 评论 0原文

    void ExecuteContent()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT TOP 1 @UsersID,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph");
     sb.Append(" FROM Users as u");
     sb.Append(" INNER JOIN Threads as t ON u.UsersID = t.UsersID");
     sb.Append(" Where @UsersID=t.UsersID");
     sb.Append(" ORDER BY t.Date DESC");


     using (SqlConnection conn = new SqlConnection(AnswerQuestion.connectionString))
     {
         conn.Open();
         SqlCommand sqlComm = new SqlCommand(sb.ToString(), conn);
        MembershipUser CurrentUser = Membership.GetUser();    
         Guid i = (Guid)CurrentUser.ProviderUserKey;
         sqlComm.Parameters.Add("@UsersID", SqlDbType.UniqueIdentifier).Value = i;
         SqlDataReader dr = sqlComm.ExecuteReader();

         UserName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
     }

}

我不明白为什么我得到它。我想做的就是找到最后一个发布该线程的人..我查看了调试,一切似乎都很好。我还查看了 Visual Studio 2010 中的 sql server.. 有数据,但不知何故它没有被读取并且抛出异常...:(

    void ExecuteContent()
{
    StringBuilder sb = new StringBuilder();
    sb.Append("SELECT TOP 1 @UsersID,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph");
     sb.Append(" FROM Users as u");
     sb.Append(" INNER JOIN Threads as t ON u.UsersID = t.UsersID");
     sb.Append(" Where @UsersID=t.UsersID");
     sb.Append(" ORDER BY t.Date DESC");


     using (SqlConnection conn = new SqlConnection(AnswerQuestion.connectionString))
     {
         conn.Open();
         SqlCommand sqlComm = new SqlCommand(sb.ToString(), conn);
        MembershipUser CurrentUser = Membership.GetUser();    
         Guid i = (Guid)CurrentUser.ProviderUserKey;
         sqlComm.Parameters.Add("@UsersID", SqlDbType.UniqueIdentifier).Value = i;
         SqlDataReader dr = sqlComm.ExecuteReader();

         UserName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
     }

}

I dont understand why I get it. All i am trying to do is to get the last person who posted the thread.. I looked at debugging, and things seem fine. I also looked at the sql server in visual studio 2010.. there is data,,but somehow it is not being read and an exception is thrown... :(

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

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

发布评论

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

评论(2

寂寞美少年 2024-11-21 20:45:27

您应该在访问值之前检查 dr.Read():

if(dr.Read())
{
    UserName = dr["Name"].ToString();//The exception is thrown here
    Image = (Image) dr["Avatar"];
    ThreadTitle = dr["ThreadTitle"].ToString();
    ThreadParagraph = dr["ThreadParagraph"].ToString();
    Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
}

You should check for dr.Read() before accessing the values:

if(dr.Read())
{
    UserName = dr["Name"].ToString();//The exception is thrown here
    Image = (Image) dr["Avatar"];
    ThreadTitle = dr["ThreadTitle"].ToString();
    ThreadParagraph = dr["ThreadParagraph"].ToString();
    Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString());
}
你的呼吸 2024-11-21 20:45:27

您必须循环遍历您的阅读器:

while(dr.Read())
{
    serName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
}

if(dr.Read()) 如果您只有一个结果

并且
你应该总是用 using: 包围你的 Reader

using(dr)
{
    while(dr.Read())
    {
    }
}

并且你应该在调用 ToString(); 之前做一些空检查。

ThreadParagraph = dr["ThreadParagraph"] == null ? "" : dr["ThreadParagraph"].ToString();

这里有一个在代码中逐字编写查询的小技巧:

var query = @"SELECT TOP 1 @UsersID,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph 
    FROM Users as u 
    INNER JOIN Threads as t ON u.UsersID = t.UsersID 
    Where @UsersID=t.UsersID ORDER BY t.Date DESC";

You must loop trough your reader:

while(dr.Read())
{
    serName = dr["Name"].ToString();//The exception is thrown here
         Image = (Image) dr["Avatar"];
         ThreadTitle = dr["ThreadTitle"].ToString();
         ThreadParagraph = dr["ThreadParagraph"].ToString();
         Time = (DateTime)AllQuestionsPresented.TryParse(dr["Date"].ToString()); 
}

or if(dr.Read()) if you only have one result

And
You should always surround you Reader with a using:

using(dr)
{
    while(dr.Read())
    {
    }
}

And you should do some null check before you call ToString();

ThreadParagraph = dr["ThreadParagraph"] == null ? "" : dr["ThreadParagraph"].ToString();

And here's a little tip to write queries in code, use verbatim:

var query = @"SELECT TOP 1 @UsersID,u.Avatar,t.Date, u.Name, t.ThreadTitle, t.ThreadParagraph 
    FROM Users as u 
    INNER JOIN Threads as t ON u.UsersID = t.UsersID 
    Where @UsersID=t.UsersID ORDER BY t.Date DESC";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文