有关数据库连接的问题????

yadianbolo 2002-04-22 07:40:51
请各位高手帮个忙吧.
我是个新手.现在要用ADO来连接一个Access的数据库.
请问一下代码要怎么写啊
谢谢.
...全文
26 点赞 收藏 6
写回复
6 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
pl5th2001 2002-04-22

Private Sub Command16_Click()
'第十六种方法使用配置好的DATA控件
Data1.Visible = False
Data1.DatabaseName = App.Path & "\db1.mdb"
Data1.RecordSource = "学生"
Data1.Refresh
With Data1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Data1.Refresh
BrwFrm.Show
End Sub

Private Sub Command17_Click()
'第十七种方法直接使用配置好的DATA
With Data1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Data1.Refresh
BrwFrm.Show

End Sub

Private Sub Command18_Click()
'第十八种方法直接使用配置好的ADODC
Adodc1.Refresh
With RecFrm.Adodc1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Adodc1.Refresh
BrwFrm.Show

End Sub

Private Sub Command19_Click()
'第十九种方法直接配置ADODC
With Adodc3
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
.CommandType = adCmdUnknown
.RecordSource = "SELECT * FROM 学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
.Refresh
End With
BrwFrm.Show

End Sub

Private Sub Command2_Click()
'第二种方法使用代码建立数据库和记录集
Dim Dat1 As Database
Dim rs As Recordset
Set Dat1 = OpenDatabase(App.Path & "\db1.mdb")
Set rs = Dat1.OpenRecordset("学生")
rs.AddNew
rs.Fields("班级") = Text1.Text
rs.Fields("学号") = Text2.Text
rs.Fields("姓名") = Text3.Text
rs.Fields("性别") = Combo1.Text
rs.Fields("出生日期") = MaskEdBox1.Text
rs.Fields("联系电话") = Text5.Text
rs.Fields("家庭住址") = Text6.Text
rs.Fields("工作单位") = Text7.Text
rs.Fields("单位电话") = Text8.Text
rs.Fields("传呼") = Text9.Text
rs.Fields("收费情况") = Text10.Text
rs.Update
Dat1.Close
BrwFrm.Show
End Sub

Private Sub Command3_Click()
'第三种方法用代码定义ADODC的连接
Dim Cnn As New ADODB.Connection
Dim Tbl As New ADODB.Recordset
Cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
With Tbl
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "学生", Cnn, , , adCmdTable
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Cnn.Close
BrwFrm.Show
End Sub

Private Sub Command4_Click()
'第四中方法修改ADODC控件的数据源
Adodc1.CommandType = adCmdTable
Adodc1.RecordSource = "学生"
Adodc1.Refresh
With Adodc1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Adodc1.Refresh
BrwFrm.Show
End Sub

Private Sub Command5_Click()
'第五中方法ADODC代码和SQL语句
Dim Cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
strSQL = "SELECT * FROM 学生"
Cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strSQL, Cnn, , , adCmdText
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Cnn.Close
BrwFrm.Show
End Sub

Private Sub Command6_Click()
'第六种 直接用SQL语句。
Dim dbs As Database
Set dbs = OpenDatabase(App.Path & "\db1.mdb")
SQLstr = "INSERT INTO 学生 (班级,学号,姓名,性别,出生日期,联系电话,家庭住址,工作单位,单位电话,传呼," _
& "收费情况) VALUES('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" _
& Combo1.Text & "','" & MaskEdBox1.Text & "','" & Text5.Text & "','" & Text6.Text & "','" & Text7.Text & "','" & Text8.Text & "','" & Text9.Text & "','" & Text10.Text & "');"
dbs.Execute SQLstr
dbs.Close
BrwFrm.Show
End Sub

Private Sub Command7_Click()
'第七中方法用代码定义一个ADODC连接
Dim Cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
strSQL = "SELECT * FROM 学生"
Cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open strSQL, Cnn, , , adCmdUnknown
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Cnn.Close
BrwFrm.Show
End Sub

Private Sub Command8_Click()
'第八中方法修改ADODC控件的数据源
Adodc1.CommandType = adCmdText
Adodc1.RecordSource = "SELECT * FROM 学生"
Adodc1.Refresh
With Adodc1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Adodc1.Refresh
BrwFrm.Show
End Sub

Private Sub Command9_Click()
'第九中方法修改ADODC的数据源
Adodc1.CommandType = adCmdUnknown
Adodc1.RecordSource = "SELECT * FROM 学生"
Adodc1.Refresh
With Adodc1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Adodc1.Refresh
BrwFrm.Show
End Sub


Private Sub Image1_Click()
'打开主页
回复
pl5th2001 2002-04-22
'指定链接
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
'第一种方法使用配置好的DATA控件
Data1.Visible = False
Data1.DatabaseName = App.Path & "\db1.mdb"
Data1.RecordSource = "select * from 学生"
Data1.Refresh
With Data1.Recordset
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
End With
Data1.Refresh
BrwFrm.Show
End Sub

Private Sub Command10_Click()
'第十种方法引用ADODC代码
Dim cnn1 As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim cnnstr As String
cnnstr = " provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & App.Path & "\db1.mdb;"
cnn1.Open cnnstr
With rst
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "学生", cnnstr, , , adCmdTable
'.Open "学生", cnn1, , , adCmdTable
.AddNew
.Fields("班级") = Text1.Text
.Fields("学号") = Text2.Text
.Fields("姓名") = Text3.Text
.Fields("性别") = Combo1.Text
.Fields("出生日期") = MaskEdBox1.Text
.Fields("联系电话") = Text5.Text
.Fields("家庭住址") = Text6.Text
.Fields("工作单位") = Text7.Text
.Fields("单位电话") = Text8.Text
.Fields("传呼") = Text9.Text
.Fields("收费情况") = Text10.Text
.Update
.UpdateBatch adAffectAllChapters
End With
cnn1.Close
BrwFrm.Show
End Sub

Private Sub Command11_Click()
'第十一种方法动态增加DATA控件
RecFrm.Controls.Add "VB.data", "data2"
With RecFrm!Data2
.Visible = False
.DatabaseName = App.Path & "\db1.mdb"
.RecordSource = "select * from 学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
.Refresh
End With
RecFrm.Controls.Remove "data2"
BrwFrm.Show
End Sub

Private Sub Command12_Click()
'第十二种方法动态增加adodc控件
RecFrm.Controls.Add "MSAdodcLib.Adodc", "adodc2"
With RecFrm!Adodc2
.Visible = False
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
.CommandType = adCmdTable
.RecordSource = "学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
End With
RecFrm.Controls.Remove "adodc2"
BrwFrm.Show
End Sub

Private Sub Command13_Click()
'第十三种方法动态增加adodc控件
RecFrm.Controls.Add "MSAdodcLib.Adodc", "adodc2"
With RecFrm!Adodc2
.Visible = False
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
.CommandType = adCmdText
.RecordSource = "SELECT * FROM 学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
End With
RecFrm.Controls.Remove "adodc2"
BrwFrm.Show

End Sub

Private Sub Command14_Click()
'第十四种方法动态增加DATA控件
RecFrm.Controls.Add "VB.data", "data2"
With RecFrm!Data2
.Visible = False
.DatabaseName = App.Path & "\db1.mdb"
.RecordSource = "学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
.Refresh
End With
RecFrm.Controls.Remove "data2"
BrwFrm.Show

End Sub

Private Sub Command15_Click()
'第十五种方法动态增加DATA控件
RecFrm.Controls.Add "MSAdodcLib.Adodc", "adodc2"
With RecFrm!Adodc2
.Visible = False
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.3.51; Data Source=" & App.Path & "\db1.mdb;"
.CommandType = adCmdUnknown
.RecordSource = "SELECT * FROM 学生"
.Refresh
.Recordset.AddNew
.Recordset.Fields("班级") = Text1.Text
.Recordset.Fields("学号") = Text2.Text
.Recordset.Fields("姓名") = Text3.Text
.Recordset.Fields("性别") = Combo1.Text
.Recordset.Fields("出生日期") = MaskEdBox1.Text
.Recordset.Fields("联系电话") = Text5.Text
.Recordset.Fields("家庭住址") = Text6.Text
.Recordset.Fields("工作单位") = Text7.Text
.Recordset.Fields("单位电话") = Text8.Text
.Recordset.Fields("传呼") = Text9.Text
.Recordset.Fields("收费情况") = Text10.Text
.Recordset.Update
.Refresh
End With
RecFrm.Controls.Remove "adodc2"
BrwFrm.Show
End Sub
回复
daryl715 2002-04-22
Dim cnn1 As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cnnstr As String
Set cnn1 = New ADODB.Connection

cnnstr = " provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\test.mdb;"
cnn1.Open cnnstr

Set rst = New ADODB.Recordset
rst.CursorType = adOpenKeyset
rst.LockType = adLockOptimistic
rst.Open "select * from stu", cnn1

Set Form1.MSHFlexGrid1.DataSource = rst

rst.Close
cnn1.Close
End Sub
回复
DragonCity 2002-04-22
引用Microsoft ActiveX Data Object2.1以上版本
Option Explicit
Dim cnn As New ADODB.Connection

Private Sub Form_Load()
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\xxxx.MDB;Persist Security Info=False;Jet OLEDB:Database Password=YourPassword"
Dim rst As New ADODB.Recordset
rst.Open "select * from table1", cnn, adOpenKeyset, adLockPessimistic, adCmdText
Set Me.MSFlexGrid1.DataSource = rst
End Sub
回复
_1_ 2002-04-22
你去那看看把, 实在太多了, 挑一个http://www.jinesc.com/myweb/disp.asp?idd=91&room=40
回复
HungryBoy 2002-04-22

strDSN = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=你的数据库路径
pConn.Open strDSN
回复
发动态
发帖子

1180

社区成员

VB 数据库(包含打印,安装,报表)
申请成为版主
社区公告
暂无公告