VB 怎么把从数据库查出来的结果显示在label中呢??

wdx222222 2009-08-16 01:06:00
Private Sub Command1_Click()
Dim Cnn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=john;UID=sa;PWD=123;initial catalog=aaa"
Cnn.Open
Rs.Open "select A from aaa", Cnn

我想把从表aaa中查询出的结果显示在label中,怎么写啊??
...全文
689 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
水猿兵团五哥 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 leftie 的回复:]
VB codePrivateSub Command1_Click()onerrorgoto errhandleDim CnnAsNew ADODB.ConnectionDim RsAsNew ADODB.Recordset
Cnn.ConnectionString="Provider=SQLOLEDB;Data Source=john;UID=sa;PWD=123;initial catalog=aaa"
Cnn.Openif rs.state<>adstateclosedthen rs.close
Rs.Open"select A from aaa", Cnn ,adopenkeyset,adlockreadonlyif rs.recordcount>0then
label1.caption=rs!a&""
label1.refreshendif
rs.close
cnn.closeexitsub'错误处理ErrHandle:
msgbox"查询失败,错误原因为:"& err.description,vbexclamation,"提示"if rs.state<>adstateclosedthen rs.closeif cnn.state<>adstateclosedthen cnn.closeend sub


[/Quote]
OK
wcabinboy 2009-08-18
  • 打赏
  • 举报
回复
楼上正解
clear_zero 2009-08-17
  • 打赏
  • 举报
回复
Label1.caption = RS!A
饮水需思源 2009-08-17
  • 打赏
  • 举报
回复
Private Sub Command1_Click() 
on error goto errhandle
Dim Cnn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=john;UID=sa;PWD=123;initial catalog=aaa"
Cnn.Open
if rs.state<>adstateclosed then rs.close
Rs.Open "select A from aaa", Cnn ,adopenkeyset,adlockreadonly
if rs.recordcount>0 then
label1.caption=rs!a & ""
label1.refresh
end if
rs.close
cnn.close
exit sub
'错误处理
ErrHandle:
msgbox "查询失败,错误原因为:" & err.description,vbexclamation,"提示"
if rs.state<>adstateclosed then rs.close
if cnn.state<>adstateclosed then cnn.close
end sub


孤独剑_LPZ 2009-08-16
  • 打赏
  • 举报
回复
Label1.caption中显示的字符数是有限的,记录多的话就显示不下了,用4楼的循环得到数据即可
ChinaITOldMan 2009-08-16
  • 打赏
  • 举报
回复
Label1.caption = RS.getstring
chinaboyzyq 2009-08-16
  • 打赏
  • 举报
回复
[Quote=引用楼主 wdx222222 的回复:]
Private Sub Command1_Click()
Dim Cnn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
          Cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=john;UID=sa;PWD=123;initial catalog=aaa"
          Cnn.Open
          Rs.Open "select  A  from  aaa", Cnn
我想把从表aaa中查询出的结果显示在label中,怎么写啊??
[/Quote]

如果有多条记录要用循环,并且放到label中可能会乱七八糟,建议改成放到TextBox中

'1、添加text1,设置multiline为true
'2、循环取查到的结果
dim tmp as string
do while not rs.eof
tmp=tmp & rs!A & vbnewline
rs.movenext
loop
'3把结果放到文本框内
text1=tmp

jingyes239 2009-08-16
  • 打赏
  • 举报
回复
Label1.caption = RS!A
slowgrace 2009-08-16
  • 打赏
  • 举报
回复
Label1.caption = RS.fields("字段名")
ldy888 2009-08-16
  • 打赏
  • 举报
回复

Private Sub Command1_Click()
Dim Cnn As New ADODB.Connection
Dim Rs As New ADODB.Recordset
Cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=john;UID=sa;PWD=123;initial catalog=aaa"
Cnn.Open
Rs.Open "select A from aaa", Cnn
Label1.caption = RS.getstring

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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