打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
ADO代码连接数据库
ADO代码连接数据库
2011-10-10 14:19

二、用ADO代码连接数据库

'在使用ADO对象前应选定Visual Basic菜单下的[工程]中的引用了菜单中的[Microsoft.ActiveX Data Object 2.5 Library]选项,或其它版本

1.'ADO代码与Access2000数据库连接

Private Sub Command1_Click()

    Dim AdoCnn As ADODB.Connection

    Dim AdoRs As ADODB.Recordset

    Set AdoCnn = New ADODB.Connection

    Set AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\chncmadb1.mdb;Jet OLEDB:DataBase PASSWORD=123456"

AdoRs.Open "select * from [耕地资源管理单元属性数据表2004]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set DataGrid1.DataSource = AdoRs

    Set AdoRs = Nothing

    Set AdoCnn = Nothing

End Sub

 

2.'ADO代码与DBF表连接

Private Sub Command2_Click()

    Dim AdoCnn As ADODB.Connection

    Dim AdoRs As ADODB.Recordset

    Set AdoCnn = New ADODB.Connection

    Set AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=MSDASQL.1;Driver=Microsoft Visual Foxpro Driver;SourceDB=" & App.Path & ";SourceType=DBF;Locale Identifier=2052"

AdoRs.Open "select * from [DBF1]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set DataGrid1.DataSource = AdoRs

    Set AdoRs = Nothing

    Set AdoCnn = Nothing

End Sub

3.'ADO代码与Excel表连接

Private Sub Command3_Click()

    Dim AdoCnn As ADODB.Connection

    Dim AdoRs As ADODB.Recordset

    Set AdoCnn = New ADODB.Connection

    Set AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\EXcel.xls;Extended Properties='Excel 8.0;HDR=Yes'"

AdoRs.Open "select * from [EXcel.xls]", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set DataGrid1.DataSource = AdoRs

    Set AdoRs = Nothing

    Set AdoCnn = Nothing

End Sub

 

4.'ADO代码与Oracle数据库连接

Private Sub Command4_Click()

    Dim AdoCnn As ADODB.Connection

    Dim AdoRs As ADODB.Recordset

    Set AdoCnn = New ADODB.Connection

    Set AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=OraOLEDB.Oracle.1;Password=chncmadb;Persist Security Info=True;User ID=chncmadb;Data Source=towebserver"

AdoRs.Open "select * from T320481TR012004", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set DataGrid1.DataSource = AdoRs

    Set AdoRs = Nothing

    Set AdoCnn = Nothing

End Sub

 

5.'ADO代码与SQLserver数据库连接

'未测试

Private Sub Command5_Click()

    Dim AdoCnn As ADODB.Connection

    Dim AdoRs As ADODB.Recordset

    Set AdoCnn = New ADODB.Connection

    Set AdoRs = New ADODB.Recordset

    AdoCnn.CursorLocation = adUseClient

    '.open后面的字符串可以参考ADO控件连接.ConnectionString后面的的字符串

    AdoCnn.Open "Provider=SQLOLEDB.1;Password=111;Persist Security Info=True;User ID=111;Initial Catalog=111;Data Source=111"

AdoRs.Open "select * from T320481TR012004", AdoCnn, adOpenDynamic, adLockPessimistic, adCmdText

    Set DataGrid1.DataSource = AdoRs

    Set AdoRs = Nothing

    Set AdoCnn = Nothing

End Sub


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VBA 使用ADO组件的两种方式
ADO技巧精华10则
VB通过ADO快速读取EXCEL与TXT文件
VB 中 ADO、 ADODB、 ADODC 的区别与联系(皮毛)
他山之石——VBA数据库操作
ACCESS 数据库-VBA(宏)程序设计(5)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服