Adodc1.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0; Data source=" & App.Path & "\data1.mdb

对象变量或with块变量未设置Adodc1.ConnectionString="provider=Microsoft.jet.OLEDB.4.0;Datasource="... 对象变量或with块变量未设置
Adodc1.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0; Data source=" & App.Path & "\data1.mdb;Persist security info= False"

Adodc1.RecordSource = "select * from admin where 用户名 = '" & Text1.Text & " '"
Adodc1.Refresh
Adodc1.Recordset.Fields("password") = Text2.Text
展开
 我来答
yin333789
推荐于2018-03-07 · TA获得超过101个赞
知道答主
回答量:120
采纳率:0%
帮助的人:79.6万
展开全部
VB与各数据库的几种连接方式
-、用DAO控件连接数据库

1.与Access2000数据库连接

Private Sub Command1_Click()

'也可直接在控件属性中设置以下各项但在控件属性中不能写入密码

'只有在数据数没有密码的情况下可以省略

Data1.Refresh

Data1.Connect = "Access 2000;"

Data1.DatabaseName = App. Path + "\chncmadb.mdb"

'数据库没有密码此句可省

Data1.Connect = ";pwd=123456"

'Data1.RecordSource = "耕地资源管理单元属性数据表2004"

Data1.RecordSource = "select * from 耕地资源管理单元属性数据表2004"

Data1.Refresh

’move后才能正确显示记录个数

End Sub

2.与没有密码的DBF文件数据库连接

Private Sub Command2_Click()

Data1.Connect = "dBASE III;"

Data1.DatabaseName = App. Path

' Data1.RecordSource ="DBF"

Data1.RecordSource = "select * from dbf"

Data1.Refresh

’move后才能正确显示记录个数

End Sub

3.与没有密码的Excel文件数据库连接

Private Sub Command3_Click()

Data1.Connect = "Excel 8.0;"

Data1.DatabaseName = App.Path & "\EXcel.xls"

Data1.RecordSource = "select * from [EXcel.xls]"

Data1.Refresh

’move后才能正确显示记录个

End Sub

二、用DAO代码连接数据库

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

1.DAO代码与Access数据库连接

Private Sub Command1_Click()

Dim Db As Database

Dim Rs As Recordset

'以共享、读写方式打开'如果无密码最后一个参数可以不要

Set Db= OpenDatabase(App.Path & "\chncmadb.mdb", False, False, ";pwd=123456")

'不需要move来更新记录个数

'Set Rs = Db.OpenRecordset("耕地资源管理单元属性数据表2004")

'需要move来更新记录个数

Set Rs = Db.OpenRecordset("select * from [耕地资源管理单元属性数据表2004]")

If Rs.RecordCount > 0 Then

Rs.MoveLast

Rs.MoveFirst

End If

End Sub

2.DAO代码与没有密码的DBF文件数据库连接

Private Sub Command2_Click()

Dim Db As Database

Dim Rs As Recordset

'以共享、读写方式打开

Set Db = OpenDatabase(App.Path, False, False, "dbase III;")

'不需要move来更新记录个数

'Set Rs = Db.OpenRecordset("DBF")

’需要move来更新记录个数

Set Rs = Db.OpenRecordset("select * from [DBF]")

If Rs.RecordCount > 0 Then

Rs.MoveLast

Rs.MoveFirst

End If

End sub

3. 'DAO代码与没有密码的Excel文件数据库连接

Private Sub Command3_Click()

Dim Db As Database

Dim Rs As Recordset

'以共享、读写方式打开'如果无密码最后一个参数可以不要

Set Db = OpenDatabase(App.Path & "\EXcel.xls", False, False, "Excel 8.0;")

'不需要move来更新记录个数

' Set Rs = Db.OpenRecordset("EXcel.xls") '表格中的工作目录sheet

'需要move来更新记录个数

Set Rs = Db.OpenRecordset("select * from [EXcel.xls]") '表格中的工作目录sheet'

If Rs.RecordCount > 0 Then

Rs.MoveLast

Rs.MoveFirst

End If

End Sub

三、用ADO控件连接数据库

'也可直接在控件属性中设置以下各项

1.ADO控件与Access2000数据库连接

Private Sub Command1_Click()

'连接有密码的Access数据库

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

'连接没有密码的Access数据库

Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\chncmadb.mdb;Persist Security Info=False"

'Adodc1.RecordSource = "[耕地资源管理单元属性数据表2004]"

Adodc1.RecordSource = "select * from [耕地资源管理单元属性数据表2004]"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

2.'ADO控件与DBF表连接

Private Sub Command2_Click()

'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=dBASE Files;DBQ=" & App.Path & ";SourceType=DBF;"

'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=Visual FoxPro Tables;UID=;SourceDB=”& app.path &”;SourceType=DBF;Exclusive=No;BackgroundFetch=Yes;Collate=Machine;Null=Yes;Deleted=Yes;"

'Adodc1.ConnectionString = "Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=dBASE Files;DBQ=”& app.path &”;;DefaultDir=”& app.path &”;DriverId=533;MaxBufferSize=2048;PageTimeout=5;"

'能使表名长度不受限制

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

'Adodc1.RecordSource = "[DBF1]"

Adodc1.RecordSource = "select * from DBF1"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

3.'ADO控件与Excel表连接

Private Sub Command3_Click()

'下面一句测试未能通过

'Adodc1.ConnectionString = "Data Provider=MSDASQL.1;driver=Microsoft Excel Driver *.xls);DBQ=" & App.Path & "\EXcel.xls"

'Adodc1.ConnectionString="Provider=MSDASQL.1;Persist Security Info=False;Extended Properties=DSN=Excel Files;DBQ=" & App.Path & "\EXcel.xls;DefaultDir=”&app.path &”;DriverId=790;MaxBufferSize=2048;PageTimeout=5;"

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

'Adodc1.RecordSource = "[EXcel.xls]"

Adodc1.RecordSource = "select * from [EXcel.xls]"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

4.'ADO控件与Oracle数据库连接

Private Sub Command4_Click()

'Adodc1.ConnectionString = "Provider=MSDAORA.1;Password=chncmadb;User ID=chncmadb;Data Source=towebserver;Persist Security Info=True"

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

'Adodc1.RecordSource = "T320481TR012004"

'表名不能加方括号

Adodc1.RecordSource = "select * from T320481TR012004"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

5.'ADO控件与SQLserver数据库连接

'未测试

Private Sub Command5_Click()

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

'Adodc1.RecordSource = "T320481TR012004"

Adodc1.RecordSource = "select * from T320481TR012004"

Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1

DataGrid1.Refresh

End Sub

四、用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
chinaboyzyq
2011-03-19 · TA获得超过1.3万个赞
知道大有可为答主
回答量:1.3万
采纳率:89%
帮助的人:3295万
展开全部
菜单命令:工程 | 部件,在部件对话框中选择Microsoft ADO Data Control 6.0 (SP6),然后把ADODC添加到窗体。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
次琳风
2011-03-19 · TA获得超过748个赞
知道小有建树答主
回答量:462
采纳率:100%
帮助的人:273万
展开全部
你们学的好难啊 ,唉,都是学计算机的苦孩子
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式