cshtml c#已经有一个打开的DataReader与此连接关联,必须先关闭它
本文关键字:关联 连接 有一个 DataReader cshtml | 更新日期: 2023-09-27 18:20:08
我在博客项目的App_Code文件夹中制作PostHelper.cshtml。我在网上收到了这个错误:
<div class="commentsTab">
<a href="@Href("~/Posts/Details/" + **Post.ID + "#comments")">@Post.comments.Count**</a>
</div>
和:
@foreach (tag Tag in **Post.tags**)
当我删除"@Post.comments.Count"时,这很好,但我有类似的行,没有错误:
<div class="postTitle"><a href="@Href("~/Posts/Details/" + Post.ID)">@Post.Title</a></div>
这个怎么了?有完整的代码:
@using Blog.Models;
@helper Render(post Post, System.Web.Mvc.HtmlHelper html, bool isAdmin, bool showComments)
{
<div class="postTitle"><a href="@Href("~/Posts/Details/" + Post.ID)">@Post.Title</a></div>
<div class="postContainer">
<div class="postTabs">
<div class="dateTab">
<div class="month">@Post.DateTime.ToString("MMM").ToUpper()</div>
<div class="day">@Post.DateTime.ToString("dd")</div>
</div>
<div class="commentsTab">
<a href="@Href("~/Posts/Details/" + Post.ID + "#comments")">@Post.comments.Count</a>
</div>
</div>
<div class="postContent">
<div class="postBody">@html.Raw(Post.Body)</div>
<div class="tagList">
@foreach (tag Tag in Post.tags)
{
<span class="tag"><a href="@Href("~/Posts/Tags/" + Tag.Name)">@Tag.Name</a></span>
}
</div>
<div class="linkList">
<div id="fb-root"></div>
<script>
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/pl_PL/sdk.js#xfbml=1&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
</div>
</div>
</div>
if (showComments)
{
<div id="commentContainer">
<a id="comments"></a>
@foreach (comment Comment in Post.comments.OrderBy(x => x.DateTime))
{
<div class="comment">
<div class="commentName">
@if (!string.IsNullOrWhiteSpace(Comment.Email))
{
<a href="mailto:@Comment.Email">@Comment.Name</a>
}
else
{
@Comment.Name;
}
</div>
said:
<div class="commentBody">@html.Raw(html.Encode(Comment.Body).Replace("'n", "<br/>"))</div>
<div class="commentTime">at @Comment.DateTime.ToString("HH:mm") on @Comment.DateTime.ToString("yyyy/MM/dd")</div>
</div>
}
<div id="commentEditor">
<div id="commentPrompt">Leave a comment!</div>
<form action="@Href("~/Posts/Comment/" + Post.ID)" method="post">
<input type="text" id="commentNamePrompt" name="name" /> Name (required)<br />
<input type="text" id="commentEmailPrompt" name="email" /> Email (optional)<br />
<textarea id="commentBodyInput" name="body" rows="10" cols="60"></textarea><br />
<input type="submit" id="commentSubmitInput" name="submit" value="Submit!" />
</form>
</div>
</div>
}
}
我的行动:
public ActionResult Index(int? id)
{
int pageNumber = id ?? 0;
IEnumerable<post> posts =
(from Post in model.posts
where Post.DateTime < DateTime.Now
orderby Post.DateTime descending
select Post).Skip(pageNumber * PostsPerPage).Take(PostsPerPage + 1);
ViewBag.IsPreviousLinkVisible = pageNumber > 0;
ViewBag.IsNextLinkVisible = posts.Count() > PostsPerPage;
ViewBag.PageNumber = pageNumber;
ViewBag.IsAdmin = IsAdmin;
return View(posts.Take(PostsPerPage));
}
我认为您的异常是由尚未关闭的与数据库的已打开连接引起的。在您的情况下,请尝试在初始选择的末尾添加一个.ToList:
select Post).Skip(pageNumber * PostsPerPage).Take(PostsPerPage + 1).ToList();
这将关闭阅读器并将所有结果复制到您的内存中。看看这是否有什么不同。
您需要MARS。将:MultipleActiveResultSets=True;
添加到您的连接字符串中。请参阅:http://msdn.microsoft.com/en-us/library/ms131686.aspx