在来之前在网上搜了很多都用了,都有错误.应该是我懂的太少了吧,以前都是学的C.现在老师让我们用VB,VB其实我们根本就没学过.
  我用的SQL server 2000,VB 6.0
先在SQL里建了一个student数据库,里面加了表 s (sname,sno)
再用VB做了登陆界面:2个Text(一个输入sname 一个输入sno) 2个command其中确认command的代码不知道该怎么写才能和SQL里面的s表做比较让后判断是否进入下个窗体.

解决方案 »

  1.   

    Dim str As String
    Dim rs  As New ADODB.RecordsetPrivate Sub Command1_Click()
    Dim student As String
    Dim i As Integer
    Dim n As Integer
    Dim cn  As New ADODB.Connection
       Set cn = New ADODB.Connection
        cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student;Data Source=6C6CF07F486A42C"
      If Text1.Text = "" Then
       MsgBox "请输入学号!", vbOKOnly + vbInformation, "用户登陆"
       Text1.SetFocus
       Exit Sub
    Else
         GetConnected
         Set rs = New ADODB.Recordset
       student = "select * from s where sname='" & Text1.Text & "'"
        rs.Open student, cn, adOpenDynamic, adLockOptimistic
       If rs.EOF = True And rs.BOF = True Then
                                      Text1.Text = ""
                                      Text2.Text = ""
                                      MsgBox "没有这个用户,请重新输入!", vbOKOnly + vbExclamation, "警告"
                                      Text1.SetFocus
                              Else
                                      If Text1.Text = rs!sname Then
                                         Text2.Text = rs!sno
                                              rs.Close
                                              Me.Hide
            Form3.Show
            Unload Me
        Else
            n = n + 1
            If n < 3 Then
                i = MsgBox("用户名或密码错误,请重新输入!", vbCritical + vbRetryCancel + vbDefaultButton1, "登录失败")
                If i = vbRetry Then
                    Text1.Text = ""
                    Text2.Text = ""
                    Text1.SetFocus
                Else
                    End
                End If
            Else
                MsgBox "三次错误,请退出!", vbOKOnly + vbExclamation, "登录失败"
                End
            End If
          End If
        End If
    End If
    End Sub这是在网上搜的我改了下,但是提示的是连接无法执行此操作.在此上下文中可能已关闭或无效.我连接数据库的连接字符串是用ADO自动生成的.我没学过VB,但是明天就要检查了.急呀
      

  2.   

    Set cn = New ADODB.Connection
        cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=student;Data Source=6C6CF07F486A42C"
    cn.open   '打开连接
      

  3.   

    能在帮忙看下么?能验证帐号,怎么验证密码也就是sno呢?应该在什么地方加点什么?