为什么我的cmd1.ExecuteReader() 读不出数据啊

dopushing 2008-01-03 11:16:24
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("userinfo.mdb"))
Dim ad As String
ad = "admin"
Dim cmd1 As New OleDbCommand("select * from bbs where username=" & ad, conn)
conn.Open()
Dim dr As OleDbDataReader = cmd1.ExecuteReader()
If dr.Read() Then
Label1.Text = dr.Item("username")
conn.Close()
End If
End Sub
...全文
587 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
dwm250 2008-01-03
  • 打赏
  • 举报
回复
username的字段类型是什么?
sjm2003 2008-01-03
  • 打赏
  • 举报
回复
Dim cmd1 As New OleDbCommand("select * from bbs where username=" & ad, conn)

这个是不是有问题啊

你直接把ad写到一起不好啊,干嘛写开,还多出两行没用的代码
Dim ad As String
ad = "admin"

建议:
你最好在数据查询前加个
try

....
end try
catch ex.ex....ion
messagebox.show (ex.message)

不然出错了也不知道是什么错误!像你这有可能是数据连接错误,也有可能是查询语句有错误!

还有就是 label1.text=dr.item("username")后面加个tostring();免得两头数据类型不相符.
llsus 2008-01-03
  • 打赏
  • 举报
回复
你的Sql语句写错了,这样修改:

Dim cmd1 As New OleDbCommand("select * from bbs where username='" & ad +"'", conn)

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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