1 Public Sub connect()
2 Dim pass As Boolean
3 pass = False
4 Dim conn As New ADODB.Connection
5 Dim rs As New ADODB.Recordset
6 Dim constr As String
7 Dim i As Integer
8 Dim sql As String
9 constr = "driver={SQL Server};server=PC-201105311336\SQLEXPRESS;Uid=sa;Pwd=123456;Database=jiaxiaoai" '无源连接
10 'constr = "dsn=sqlserver;Uid=sa;Pwd=123456;Database=jiaxiaoai" '有源连接
11 conn.Open(constr)
12 sql = "select * from sysname"
13 'rs.Open(sql, conn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockReadOnly)
14 rs.Open(sql, conn, 1, 1)
15 If conn.State = 1 Then
16 For i = 1 To rs.RecordCount
17 If rs.Fields("name").Value = usertext.Text And rs.Fields("password").Value = passwordtext.Text Then
18 pass = True
19 Else
20 If rs.EOF = True Then
21 Exit For
22 rs.MoveNext()
23 End If
24 End If
25 Next
26 If pass = True Then
27 Me.Hide()
28 MsgBox("登陆成功")
29 Else
30 MsgBox("用户名或密码错误")
31 End If
32 Else
33 MsgBox("连接失败:state=" & conn.State)
34 End If
35
36 End Sub
注释掉的表示可以替换,
可以把rs.Fields("name").Value = usertext.Text And rs.Fields("password").Value = passwordtext.Text
替换成rs(0).Value = usertext.Text And rs(1).Value = passwordtext.Text //0或1表示列号,列号是从0开始
rs(1).name 取出列名即字段名
rs(1).type 取出字段类型
rs(1).value 取出字段对应的值
rs.fields("字段名").value 取出字段值