用ADO对象(非ADO控件绑定)做的数据库查询,如果我想把查询结果显示在DataGrid之类的控件上,那么相应控件的DataSource属性该怎么设置?以下是我写的测试程序,请大虾指教。
Option Explicit
Dim conTest As New ADODB.Connection
Dim cmdSci As New ADODB.Command
Dim rsSci As New ADODB.Recordset
Private Sub Form_Load()
Dim DBpath As String
Dim strConn As String
Dim strSQL As String
DBpath = App.Path
If Right(DBpath, 1) <> "\" Then DBpath = DBpath & "\"
DBpath = DBpath & "Test.mdb"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBpath & ";Persist Security Info=False"
conTest.ConnectionString = strConn
conTest.Open
cmdSci.ActiveConnection = conTest
strSQL = "select * from Scientist where Country = 'China'"
cmdSci.CommandText = strSQL
rsSci.Open cmdSci, , adOpenDynamic, adLockBatchOptimistic
dgTest.DataSource = rsSci '执行到此处提示说.DataSource成员找不到
dgTest.Refresh
End Sub
以上dgTest是一个DataGrid控件,若改用ADODC绑定数据库则结果可以在DataGrid上正确显示。
Option Explicit
Dim conTest As New ADODB.Connection
Dim cmdSci As New ADODB.Command
Dim rsSci As New ADODB.Recordset
Private Sub Form_Load()
Dim DBpath As String
Dim strConn As String
Dim strSQL As String
DBpath = App.Path
If Right(DBpath, 1) <> "\" Then DBpath = DBpath & "\"
DBpath = DBpath & "Test.mdb"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBpath & ";Persist Security Info=False"
conTest.ConnectionString = strConn
conTest.Open
cmdSci.ActiveConnection = conTest
strSQL = "select * from Scientist where Country = 'China'"
cmdSci.CommandText = strSQL
rsSci.Open cmdSci, , adOpenDynamic, adLockBatchOptimistic
dgTest.DataSource = rsSci '执行到此处提示说.DataSource成员找不到
dgTest.Refresh
End Sub
以上dgTest是一个DataGrid控件,若改用ADODC绑定数据库则结果可以在DataGrid上正确显示。
解决方案 »
- 分不多了,求救Internet Transfer Control的问题,急,在线等!
- 我写了一个程序经常与要连接的网络设备掉线呀,是什么原因呀
- 如何移控件,帮帮忙吧!!
- 请教?
- 请教各位大侠:关于 vsPrinter 换纸张的问题?
- 急:关于VB串口通信
- 请问如何用VB得知一个Excel表的有内容的最大行号及列号,我想给他们再加上边框
- 谁知道True DBGrid Pro 7.0的注册码,发到[email protected]
- SSTab控件中,不同页面里的控件出现重叠?
- 用Filter过滤数据,过滤条件能不能是变量--菜鸟提问
- 哪有XP风格的toolbar,谢谢
- 怎样将大图片转换成小图片存入数据库,已减少占用空间
“工程”-->“引用”-->“Microsoft AxtiveX Data Objects 2.X Library”
注:2.X为版本号,如果你机子上有高版本的就用高版本的,如:2.5或2.6的。
"工程"-->“部件”-->“MicroSoft DataGrid Control 6.0”
示例:
Private Sub Command1_click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim cnstr As String
cnstr = oConn.Open "Driver={Microsoft dBASE Driver (*.dbf)};" & _
"DriverID=277;" & _
"Dbq=" & app.path & "\data"
cn.Open cnstr
rs.CursorLocation = adUseClient'使用客户端游标
rs.Open "select * from XXX.DBF", cn, adOpenKeyset, adLockBatchOptimistic
set datagrid1.datasource=rs
datagrid1.refresh
End Sub以上示例程序的作用是将XXX.dbf表中的记录显示在datagrid控件中。
dim conTest As New ADODB.Connection
dim cmdSci As New ADODB.Command
dim rsSci As New ADODB.Recordset
Private Sub Form_Load()
Dim DBpath As String
Dim strConn As String
Dim strSQL As String
DBpath = App.Path
If Right(DBpath, 1) <> "\" Then DBpath = DBpath & "\"
DBpath = DBpath & "Test.mdb"
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DBpath & ";Persist Security Info=False"
conTest.ConnectionString = strConn
conTest.Open
cmdSci.ActiveConnection = conTest
strSQL = "select * from Scientist where Country = 'China'"
cmdSci.CommandText = strSQL
rsSci.CursorLocation = adUseClient'注意:使用客户端游标
rsSci.Open cmdSci, , adOpenDynamic, adLockBatchOptimistic
set dgTest.DataSource = rsSci '执行到此处提示说.DataSource成员找不到,你少写了一个set
dgTest.Refresh
End Sub