VB中如何访问所有的记录,为何我只能访问到第一条记录

来源:百度知道 编辑:UC知道 时间:2024/04/30 14:15:49
Private Sub cmdOK_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
cn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=g01;Data Source=Localhost"
sql = "select * from ID"
rs.CursorLocation = adUseClient
rs.Open sql, cn, 1, 1
Select Case rs("ID")
Case txtUserName.Text
Select Case rs("password")
Case txtPassword.Text
Form1.Show
Case Else
MsgBox "密码不对", vbDefaultButton1 + 48, "错误"
End Select
Case Else
MsgBox "用户名不对", vbDefaultButton1 + 48, "错误"
End Select
End Sub
这是制作一个登陆框的代码,当然我知道另一种方法,那是一种普遍的方法,我只是觉得为什么我这段代码只能核对第一条记录,而下面的记录访问不到。

Private Sub cmdOK_Click()
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
cn.Open "Provider=SQLOLEDB.1;Password=sa;Persist Security Info=True;User ID=sa;Initial Catalog=g01;Data Source=Localhost"
sql = "select * from ID"
rs.CursorLocation = adUseClient
rs.Open sql, cn, 1, 1

while not rs.eof
if rs("id") = txtUserName.Text then
if rs("password")=txtPassword.Text then
MsgBox "验证通过"
exit sub
else
MsgBox "密码不对", vbDefaultButton1 + 48, "错误"
exit sub
end if
end if
wend
rs.close
MsgBox "用户名不对", vbDefaultButton1 + 48, "错误"
end sub