首先引用:Microsoft ActiveX Data Objects 2.5 Library(msado25.dll)或其他版本ADO,以下code仅供参考:Dim kkk As New ADODB.Connection
Dim lll As New ADODB.Recordsetkkk.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Persist Security Info=False"
lll.CursorLocation = adUseClient
lll.Open "select * from table2 ", kkk, adOpenKeyset, adLockOptimistic
Set Text1.DataSource = lll
Text1.DataField = "Field1"详细用法参考msdn
Dim lll As New ADODB.Recordsetkkk.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\db1.mdb;Persist Security Info=False"
lll.CursorLocation = adUseClient
lll.Open "select * from table2 ", kkk, adOpenKeyset, adLockOptimistic
Set Text1.DataSource = lll
Text1.DataField = "Field1"详细用法参考msdn
谢谢!!
你可以直接引用ADO对象
如:
dim cn as adodb.connection
dim rs as adodb.recordset
cn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & app.path & "xxx.mdb"
rs.open "select * ................" ,cn,2,3
rs.movefirst
txtNo.text=rs!字段名或者rs("字段名")引用就可以了
下一条就是rs.movenext
当然要判断是不是到了最后
Dim cn As rdoConnection
Dim ps As rdoPreparedStatement
Dim rs As rdoResultset
Dim strConnect As String
Dim strSQL As String
'利用 DSNless 连接到 pubs 数据库
'改变参数以适合你自己的 SQL Server
strConnect = "Driver={SQL Server}; Server=myserver; " & "Database=pubs; Uid=sa; Pwd="
Set cn = rdoEnvironments(0).OpenConnection(dsName:="", Prompt:=rdDriverNoPrompt, ReadOnly:=False, Connect:=strConnect)
首先引用activex data object 2.6 library
option explicit
dim conn as new adodb.connection
dim rs as new adodb.recordset
private sub form_load()
dim connstr as string
dim ssql as string
connstr="driver={sql server};server=(local);database=数据库名称;uid=sa;pwd=密码"
conn.open ssql
rs.open "select * from 数据表名字",conn,3,3
text1.text=rs.fields("字段名称")
rs.close
conn.close
end sub
Dim ps As rdoPreparedStatement
Dim rs As rdoResultset
Dim strConnect As String
Dim strSQL As String
'利用 DSNless 连接到 pubs 数据库
strConnect = "Driver={SQL Server}; Server=myserver; " & "Database=pubs; Uid=sa; Pwd="
Set cn = rdoEnvironments(0).OpenConnection(dsName:="", Prompt:=rdDriverNoPrompt, ReadOnly:=False, Connect:=strConnect)
Dim adoCN As New ADODB.Connection '定义数据库的连接
Dim strCnn As New ADODB.Recordset
str1 = "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=wktrade;Data Source=net5"
adoCN.Open str1
End Sub
Public SqlCommand As New ADODB.Command '定义 SQL 命令
Dim adoDateTime As New ADODB.Recordset '获取 NT-SERVER 时间Public Function OpenConnection() As String '打开数据库
On Error GoTo SQLConErr
With adoCN
.CursorLocation = adUseClient
.Provider = "sqloledb"
.Properties("Data Source").Value = cNtServerName
.Properties("Initial Catalog").Value = cDatabaseName
.Properties("User ID") = cSQLUserName
.Properties("Password") = cSQLPassword
.Properties("prompt") = adPromptNever
.ConnectionTimeout = 15
.Open
If .State = adStateOpen Then
adoDateTime.Open "select getdate()", adoCN, adOpenStatic, adLockOptimistic
cServerDate = Format(adoDateTime(0), "yyyy-mm-dd")
cServertime = Mid(adoDateTime(0), 10)
Else
MsgBox "数据库连接失败,请找系统管理员进行检查 !", 16, cProgramName
End
End If
End With
SqlCommand.ActiveConnection = adoCN
SqlCommand.CommandType = adCmdText
Exit Function
SQLConErr:
Select Case Err.Number
Case -2147467259
MsgBox "找不到指定的SQL Server服务器或者数据库不存在,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case -2147217843
MsgBox "指定的SQL Server数据库用户不存在或口令错误,请重新设置!", vbExclamation
F_SetSystem.Show 1
Case Else
MsgBox "数据环境连接失败,请找系统管理员进行检查 !", 16, cProgramName
End Select
OpenConnection
End Function
Dim cnn2 As ADODB.Connection
cnn2.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa用户名;password=li密码;Initial Catalog=userdata;Data Source=ly数据库所在主机名"
cnn2.Open