vb中用ado对象Recordset来绑定到DataGrid的常见问题分析



vb中用ado对象Recordset来绑定到DataGrid的常见问题分析

2014年03月14日 ⁄ 综合 ⁄ 共 2412字 ⁄ 字号 ⁄ 评论关闭
-

先看正常 代码:
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
' Call FillDataToMSFlex(Gs, MSFlexGrid1)
Set  Gs  =   N othing
Set  con  =   Nothing

 
现在做如下修改:

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
' Call FillDataToMSFlex(Gs, MSFlexGrid1)
'
Set Gs = Nothing
'
Set con = Nothing
Gs.Close
con.Close

会出现datagrid里没有数据.
那我再想Set gs=nothing 和gs.close到底有什么区别,我看了下百度Nothing,如下就是其解释:

Nothing 关键字用于取消某对象变量与实际对象的关联。使用 Set 语句将对象变量赋值为 Nothing。例如:

Set MyObject = Nothing
多个对象变量可以引用同一个实际对象。当 Nothing 赋值给某对象变量后,此变量就不再引用任何实际对象。当多个对象变量引用同一个对象时,只有在所有对象变量都设置为 Nothing (在设置为 Nothing 的最后一个对象变量超出范围后,显式或隐式地使用 Set 命令)后,与变量所引用的对象相关联的内存和系统资源才被释放。

再如果不写con.CursorLocation = adUseClient,也没有数据,并且会报错,它会提示行集合不能做为标签.

故我感想:
datagrid太不理想了,因为它的数据是需要绑定的,而不是想MSFlexGrid那样是从数据集里读取的.

Public   Sub  FillDataToMSFlex(rs  As  ADODB.Recordset, Mygrid  As  MSFlexGrid)  ' 将数据填充到MSFLEXGRID控件中去
  Dim  i, j  As   Integer
 
Dim  RowCount  As   Integer
 Mygrid.AllowUserResizing 
=  flexResizeBoth
 
Do  Until rs.EOF()
                Mygrid.Cols 
=  rs.Fields.Count  +   1
                Mygrid.TextMatrix(
0 0 =   " 序号 "
                
For  i  =   1   To  rs.Fields.Count
                    Mygrid.TextMatrix(
0 , i)  =  rs.Fields(i  -   1 ).Name
                
Next  i
                Mygrid.Rows 
=  RowCount  +   1
                Mygrid.TextMatrix(RowCount, 
0 =  RowCount
                
For  j  =   0   To  rs.Fields.Count  -   1
                    
If   IsNull (rs.Fields(j))  Then
                        Mygrid.TextMatrix(RowCount, j 
+   1 =   " NULL "
                    
Else
                        Mygrid.TextMatrix(RowCount, j 
+   1 =  rs.Fields(j)
                    
End   If
                
Next  j
                
' Mygrid.TextMatrix(RowCount, j + 1) = Trim(RecordValue(j))
                RowCount  =  RowCount  +   1
                rs.MoveNext
        
Loop
End Sub

MsFlexGrid它没有datagrid那样娇贵,这不行,那样也不行,只要有个数据集即可.

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值