怎么把RecordSet中的数据绑定到DataGrid中啊?提示 类型不匹配

cart55free99 2011-12-26 11:58:03
Private Sub Command_testSQL_Click()
Set rs = New ADODB.Recordset
Dim cmd As ADODB.Command
Dim arr As Variant
Dim sql As String

sql = "select * from tb_reader where stuid=? and stuname=? "
Set cmd = New ADODB.Command

With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = sql
arr = Array(101, "101")
.Parameters(0).Value = arr(0)
.Parameters(1).Value = arr(1)
rs = .Execute
End With

Dim str1 As String
str1 = rs("stuid")
MsgBox str1, 64 '应该不是连接的问题 因为查询数据返回的结果还是蛮正常的


Set DataGrid_reader.DataSource = rs
'Set Adodc_rs.Recordset = rs ' 这2句都不行

End Sub


Microsoft ActiveX Data Object 2.5 Library 这个我已经因用过了...
...全文
75 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldy 2012-02-03
  • 打赏
  • 举报
回复
With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = sql
arr = Array(101, "101")
.Parameters(0).Value = arr(0)
.Parameters(1).Value = arr(1)
rs = .Execute'这句应该有问题吧,set
End With
xihuanxiatian 2012-02-03
  • 打赏
  • 举报
回复
这段代码也许能帮到LZ,LZ可以试试
Dim con As New ADODB.Connection ''定义数据库的连接存放数据和代码
Dim cmd As New ADODB.Command
Dim Gs As New ADODB.Recordset
Dim strSQL As String
con.ConnectionString = StrConn
strSQL = "select * from 顾客信息表"
''con.CursorLocation = adUseClient''无就不能绑定数据了?
con.CursorLocation = adUseClient ''还不能是adUseServer
con.Open
Set Gs = con.Execute(strSQL)
''Gs.Open strSQL, con, 3, 3
Set DataGrid1.DataSource = Gs
DataGrid1.Refresh

con.CursorLocation = adUseClient ''还不能是adUseServer
这句应该是关键了
不过
datagrid太不理想了,因为它的数据是需要绑定的,而不是想MSFlexGrid那样是从数据集里读取的.

Public Sub FillDataToMSFlex(rs As ADODB.Recordset, Mygrid As MSFlexGrid) ''将数据填充到MSFLEXGRID控件中去
也许换个控件也不错
xihuanxiatian 2012-02-03
  • 打赏
  • 举报
回复
这个我也一直很想知道为什么
Set DataGrid_reader.DataSource = rs

这句绑定为什么不行

加上一句绑定 刷新好像也不行
不过lz可以试着加上绑定和刷新的语句试试
lilbchn 2012-02-03
  • 打赏
  • 举报
回复
Private Sub Command_testSQL_Click()
Set rs = New ADODB.Recordset
Dim cmd As ADODB.Command
Dim arr As Variant
Dim sql As String

sql = "select * from tb_reader where stuid=? and stuname=? "
Set cmd = New ADODB.Command

With cmd
.ActiveConnection = cnn
.CommandType = adCmdText
.CommandText = sql
arr = Array(101, "101")
.Parameters(0).Value = arr(0)
.Parameters(1).Value = arr(1)
rs.open cmd, , adOpenStatic, adLockOptimistic
End With

Dim str1 As String
str1 = rs("stuid")
MsgBox str1, 64 '应该不是连接的问题 因为查询数据返回的结果还是蛮正常的


Set DataGrid_reader.DataSource = rs
'Set Adodc_rs.Recordset = rs ' 这2句都不行

End Sub

7,763

社区成员

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

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