
刚开始写程序发现语法变得好多啊 边研究边试...可算是把登陆写好了...分享下吧.
总体来说写着有点懵圈。。
Module Module2
Public conn As ADODB.Connection '声明记录集
Public rs As ADODB.Recordset '声明结果集
Public Link As String '定义link数据库字符串
Public Loginname As String '定义登陆窗口名字返回值
Public loginpass As String '定义登陆窗口密码返回值
Public loginid As String '定义登陆窗口编号返回值
Public jg As String '定义注册成功后返回的结果
Public Sub Lj()
Dim DbUser As String, DbPw As String
Dim DbName As String, DbIP As String
DbUser = "sa" '用户名
DbPw = "1" '密码
DbName = "demon" '数据库名
DbIP = "127.0.0.1" '服务器地址
Link = "driver={sql server}"
Link = Link & ";server=" & DbIP
Link = Link & ";uid=" & DbUser
Link = Link & ";pwd=" & DbPw
Link = Link & ";Database=" & DbName
conn = New ADODB.Connection
rs = New ADODB.Recordset '建立无源数据库连接
conn.ConnectionString = Link '字符串赋值
conn.ConnectionTimeout = 50 '反应时间
conn.Open '打开记录源
rs.ActiveConnection = conn '设置游标类型
rs.CursorType = ADODB.CursorTypeEnum.adOpenDynamic '游标类型
End Sub
Public Function SelLogin(sjk As String, id As String, password As String, name As String) '登陆界面添加
Dim sql As String
sql = "select * from " & sjk & " where id='" & id & "'" '查询ID
rs.Open(sql, conn)
If rs.EOF = True Then '如果这个末尾是空
Loginname = "" '返回值赋空
Else
Loginname = Trim(rs.Fields("name").Value) '如果有记录 返回值赋值回去
loginid = Trim(rs.Fields("id").Value)
loginpass = Trim(rs.Fields("password").Value)
End If
End Function
End Module
Public Class Form1
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Or TextBox2.Text = "" Then
MsgBox("用户名编号或密码没有填写!", vbOKOnly + vbExclamation, "警告")
Else
Call Lj()
Call SelLogin("login_user", TextBox1.Text, TextBox2.Text, Label1.Text)
If rs.EOF = True Then
MsgBox("没有这个用户ID,请重新输入用户名!", vbOKOnly + vbExclamation, "警告")
TextBox1.Focus()
Else
If (Trim(TextBox2.Text)) = loginpass Then
Form2.Show()
Else
MsgBox("输入的密码不正确!", vbOKOnly + vbExclamation, "警告")
TextBox2.Focus()
TextBox2.SelectAll()
End If
End If
End If
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = "" Then
Exit Sub
Else
Call Lj() '链接数据库
Call SelLogin("login_user", TextBox1.Text, TextBox2.Text, Label4.Text)
Label4.Text = Loginname '读取操作员名称
End If
End Sub
End Class