vb无源连接数据库,发生错误~
数据库连接不上Option Explicit
Public LoginSucceeded As Boolean
'引用Microsoft ActiveX Data Objects 2.6 Library
'定义连接对象和记录集
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
'对取消按钮单击事件的响应
Private Sub command2_Click()
'设置全局变量为 false
'不提示失败的登录
LoginSucceeded = False
Me.Hide
End Sub
'对登录按钮单击事件的响应
Private Sub command1_click()
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
'建立无源数据库连接
conn.ConnectionString = "driver={sql server};server=(local);uid=sa;pwd= ;Database=course"
conn.ConnectionTimeout = 50
conn.Open
Dim str As String
'连接连接对象
Set rs.ActiveConnection = conn
'设置游标类型
rs.CursorType = adOpenDynamic
If Trim(Text1.Text = "") Then
MsgBox "用户名不能为空,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
'设置查询字符串
str = "select * from students where sno ='" & Trim(Text1.Text) & "’”"
rs.Open str
If rs.EOF = True Then
MsgBox "没有这个用户,请重新输入用户名!", vbOKOnly + vbExclamation, "警告"
Text1.SetFocus
Else
'登录成功,连接主窗口
If (Trim(Text2.Text) = Trim(rs.Fields("password"))) Then
LoginSucceeded = True
Me.Hide
Form2.Show
Else
MsgBox "密码不正确,请重输入密码!", , "警告"
Text2.SetFocus
SendKeys "{Home}+{End}"
End If
End If
End If
End Sub