我是ASP.NET的新手,作为一个自我选择的任务,我将创建一个简单的博客。它已经接近完成,但我遇到了一个问题。从代码中可以看出,我首先使用查询从SQL Server数据库中返回博客条目的数量。然后,for循环从所有行读取标题,日期和文本内容,并创建一个html div,并将其加载到其中。 除了一件事之外,它的作用很好:ExecuteScalar()只读取第一行,它会为每次迭代返回相同的值,导致所有博客条目都相同。
我需要一种让读者继续进行每次迭代的方法。我已经考虑过将SQLbulkcopy放到另一个表中,然后我可以删除for循环中的顶行,但是如果要在每个Page_Load上完成它,这似乎会减慢页面的速度。
string SQLreadTitle = "Select BLOG.BlogTitle from BLOG order by BlogID DESC";
string SQLreadDate = "Select BLOG.BlogDate from BLOG order by BlogID DESC";
string SQLreadText = "Select BLOG.BlogText from BLOG order by BlogID DESC";
string SQLcountIDs = "Select count(BlogID) from BLOG";
protected void Page_Load(object sender, EventArgs e)
{
int i;
SqlConnection con = new SqlConnection(cnString);
con.Open();
SqlCommand countIDs = new SqlCommand(SQLcountIDs, con);
int count = Convert.ToInt32(countIDs.ExecuteScalar());
for (i = 0; i < count; i++)
{
SqlCommand readTitle = new SqlCommand(SQLreadTitle, con);
string titleString = readTitle.ExecuteScalar().ToString();
SqlCommand readDate = new SqlCommand(SQLreadDate, con);
string dateString = readDate.ExecuteScalar().ToString();
SqlCommand readText = new SqlCommand(SQLreadText, con);
string textString = readText.ExecuteScalar().ToString();
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "BlogPost";
dynDiv.InnerHtml = "<div id=\"BlogTitle\">" +
titleString + "</div><br /><div id=\"BlogDate\">"
+ dateString + "</div><br /><br /><div id=\"BlogText\">"
+ textString + "</div>";
Label1.Controls.Add(dynDiv);
}
con.Close();
}
提前致谢!
答案 0 :(得分:5)
你对自己的生活太过艰难。您应该有一个select语句,然后读取返回的每一行。
string SQL = "Select BLOG.BlogTitle, BLOG.BlogDate, BLOG.BlogText from BLOG order by BlogID DESC";
// Positions of the columns you are reading
const int TITLE_ORDINAL = 0;
const int DATE_ORDINAL = 1;
const int TEXT_ORDINAL = 2;
protected void Page_Load(object sender, EventArgs e)
{
using (var con = new SqlConnection(cnString))
{
con.Open();
try
{
using (var oCommand = new SqlCommand(SQL, con))
{
using (var oReader = oCommand.ExecuteReader())
{
while (oReader.Read())
{
var dynDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "BlogPost";
dynDiv.InnerHtml = "<div id=\"BlogTitle\">" +
oReader.GetString(TITLE_ORDINAL) + "</div><br /><div id=\"BlogDate\">"
+ oReader.GetString(DATE_ORDINAL) + "</div><br /><br /><div id=\"BlogText\">"
+ oReader.GetString(TEXT_ORDINAL) + "</div>";
Label1.Controls.Add(dynDiv);
}
}
}
} finally {
con.Close();
}
}
}
答案 1 :(得分:1)
我认为你已经阅读了更多关于ADO.NET的内容,因为你有一些错误的概念。 SqlCommand.ExecuteScalar用于从数据库中获取单个值,而不是获取一组数据。
您无需获取项目计数来迭代记录集。使用SqlCommand.ExecuteReader,您可以在单个查询中获得所需的所有数据。 在这里,您可以使用示例代码
string sqlQuery = "Select * from BLOG order by BlogID DESC";
using (SqlConnection con = new SqlConnection(cnString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(sqlQuery, con))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl dynDiv =
new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
dynDiv.ID = "BlogPost";
dynDiv.InnerHtml = "<div id=\"BlogTitle\">" +
dr["BlogTitle"] + "</div><br /><div id=\"BlogDate\">"
+ dr["BlogDate"].ToString() + "</div><br /><br /><div id=\"BlogText\">"
+ dr["BlogText"] + "</div>";
Label1.Controls.Add(dynDiv);
}
}
}
}
Here你有一篇MSDN文章可以帮助你理解如何使用SqlDataReader
答案 2 :(得分:0)