vb连接数据库

来源:互联网 发布:c语言打印字母图形 编辑:程序博客网 时间:2024/05/01 18:57
Private Sub cmd1_Click()
    Dim cnn As ADODB.Connection
  Dim my_recordset As ADODB.Recordset
  Dim connect_string As String
  Dim statestring As String
  Set cnn = New ADODB.Connection
  Set my_recordset = New ADODB.Recordset
  '连接Access数据库
  connect_string = "DSN=medic_lib97;UID=;PSW="
  cnn.Open connect_string
  Select Case cnn.State
    Case adStateClose
         statestring = "adStateClosed"
    Case adStateOpen
         statestring = "adStateOpen"
  End Select
  '显示连接的状态
  MsgBox "连接成功!", , statestring

    If Trim(Combo1.Text = "") Then
        MsgBox ("用户名不能为空,请重新输入!")
        txt2.Text = ""
        Combo1.SetFocus
    Else
        If Trim(txt2.Text = "") Then
            MsgBox ("密码不能为空,请重新输入!")
            txt2.Text = ""
            txt2.SetFocus
        Else
            my_recordset.Open "select * from user where id='" & Combo1.Text & "'", cnn
            If my_recordset.EOF = True Then
                MsgBox ("用户名不存在!")
                Combo1.Text = ""
                txt2.Text = ""
                Combo1.SetFocus
             Else
                If (Trim(txt2.Text) = my_recordset.Fields("passwd")) Then
                    Me.Hide
                    Frm_main.Show
                    'MsgBox ("登陆成功!")

                 Else
                     MsgBox ("密码错误,登录失败!")
                     txt2.Text = ""
                     txt2.SetFocus
End If
End If
End If
End If
End Sub
原创粉丝点击