初学用C#连接数据库的问题

来源:百度知道 编辑:UC知道 时间:2024/09/21 13:50:27
望高手帮我解决以下问题,以下代码要是放在script里的话那就可以运行,要是放在aspx.cs文件里的话就出现问题,望高手帮忙解决,万分感激啊。
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conPubs;
SqlCommand cmdSelectAuthors;
SqlDataReader dtrAuthors;

conPubs = new SqlConnection(@"Server=localhost;Integrated Security=SSPI;database=pubs");
conPubs.Open();
cmdSelectAuthors = new SqlCommand("Select au_lname From Authors", conPubs);
dtrAuthors = cmdSelectAuthors.ExecuteReader();
while (dtrAuthors.Read())
{
Response.Write("<li>");
Response.Write(dtrAuthors["au_lname"]);
}
dtrAuthors.Close();
conPubs.Close();
}
我不懂得解决,真是郁闷啦,用vb来写也出现过这个问题

贴错误吧。这确实不好说
是不是没有引如命名空间的

........
System.Text.StringBuilder st = new System.Text.StringBuilder();

while (dtrAuthors.Read())
{
st.Append("<li>");
st.Append(dtrAuthors["au_lname"].ToString());
}
Response.Write(st);
.......

报什么错?
自己先设断点走一遍,尝试解决
应该不是什么大问题

加这个试试:
if(!page.ispostback)
{
//把代码加到里面
}

你把 报错的 问题 贴出来 看看