读取数据无法读取出来。

kevin52141 2008-07-18 11:44:38
我通过GridView来读取出当前行的ID了,

protected void LinkButton1_Click(object sender, EventArgs e)
{
string sid;
sid = ((LinkButton)sender).CommandArgument.ToString(); //这里获取到ID,已经测试输出sid的值为该行ID
string sql;
SqlConnection myconn;
SqlCommand mycommand;
myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbs"].ConnectionString);
sql = "select tname from users where id = '" + sid +"'" ;
mycommand = new SqlCommand(sql, myconn);
myconn.Open();
SqlDataReader aaa = mycommand.ExecuteReader();
Response.Write(aaa);



}


现在输出aaa的值无法输出,我很菜希望能帮忙。分数少见谅
...全文
46 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
kevin52141 2008-07-19
  • 打赏
  • 举报
回复
我按照楼上两位的做法做了,现在调试的时候出错信息显示为

用户代码未处理 在没有任何数据时做无效的读取尝试
Jade_2008 2008-07-19
  • 打赏
  • 举报
回复
输出错误...
flyfly_lucky 2008-07-19
  • 打赏
  • 举报
回复

protected void LinkButton1_Click(object sender, EventArgs e)
{
string sid;
sid = ((LinkButton)sender).CommandArgument.ToString(); //这里获取到ID,已经测试输出sid的值为该行ID
string sql;
SqlConnection myconn;
SqlCommand mycommand;
SqlDataReader aaa;
myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbs"].ConnectionString);
sql = "select tname from users where id = '" + sid +"'" ;
mycommand = new SqlCommand(sql, myconn);
myconn.Open();
aaa = mycommand.ExecuteReader();
While(aaa.Read())
{
Response.Write(aaa[i].ToString());
}


}
honghu3000 2008-07-18
  • 打赏
  • 举报
回复
Response.Write(aaa["tname"].ToString());
aaa是一个sqldatareader对象,不能直接那样输出
lonelygames 2008-07-18
  • 打赏
  • 举报
回复

其实你可以断点跟踪一下 SqlDataReader aaa = mycommand.ExecuteReader();
看aaa是什么,我想应为null;
sql = "select tname from users where id = '" + sid +"'" ;
改为:
sql = "select tname from users where id = '" + sid.Trim() +"'" ;
试试
要么你只有断点跟踪了。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧