如何用VB访问数据库
来源:百度知道 编辑:UC知道 时间:2024/06/21 14:48:49
我是新学习VB的,能不能把开始连接数据库的代码注释一下。非常感谢。
cn.ConnectionString = "provider=Microsoft.jet.OLEDB.4.0;" & _
"Persist security info= False; Data source=" & App.Path & "\Daily.mdb"
cn.Open
SQL = "select * from Login where UserName='" & Trim$(txtuser.Text) & "'" _
& "And Password='" & Trim$(txtpassword.Text) & "'"
rs.CursorLocation = adUseClient
rs.Open Trim$(SQL), cn, adOpenKeyset, adLockPessimistic
If rs.RecordCount = 0 Then
i = i + 1
以下我介绍一个比较简单的写法。首先须引用Projects-Components-Microsoft ADO Data Controls 6.0 (OLEDB).
之后在表单设置ADODC控件。然后输入以下代码:
方法1:
Private Sub cmdlogin_Click()
If txtuser = "" Then
MsgBox "Please fill in User Name.", vbInformation + vbOKOnly, "Information"
txtuser.SetFocus
Exit Sub
End If
If txtpassword = "" Then
MsgBox "Please fill in Password.", vbInformation + vbOKOnly, "Information"
txtpassword.SetFocus
Exit Sub
End If
Dim SQL As String
Adodc1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & App.Path & "\数据库名称.mdb"
Adodc1.CursorLocation = adUseClient
Adodc1.CommandType = adCmdText
SQL = "select * from 资料表名称 where UserName='" & Trim$(txtuser.Text) & "'And Password='" & Trim$(txtpassword.Text) & "'&qu