登录系统,程序如下:
Public Class LoginForm
'记录用户登录次数
Private MyTryCount As Integer = 0
Private Sub 登录系统Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 登录系统Button.Click
Dim MySQLConnectionString As String = My.Settings.MyAssetsConnectionString
Dim MySQL As String = "Select COUNT(*) From 系统用户及权限表 Where 用户名称=@用户名称 AND 用户密码=@用户密码"
Dim MyConnection As New SqlConnection(MySQLConnectionString)
MyConnection.Open()
Dim MyCommand As New SqlCommand(MySQL, MyConnection)
MyCommand.Parameters.Clear()
Dim MySqlNameParameter As New SqlParameter()
MySqlNameParameter.ParameterName = "@用户名称"
MySqlNameParameter.Value = Me.用户名称TextBox.Text
MyCommand.Parameters.Add(MySqlNameParameter)
Dim MySqlPWParameter As New SqlParameter()
MySqlPWParameter.ParameterName = "@用户密码"
MySqlPWParameter.Value = Me.用户密码TextBox.Text
MyCommand.Parameters.Add(MySqlPWParameter)
Dim MyCount As Integer = MyCommand.ExecuteScalar()
If (MyCount = 1) Then
Me.Close()
Else
Me.MyTryCount += 1
If (Me.MyTryCount <> 3) Then
Dim MyDoCount As Integer = 3 - Me.MyTryCount
Dim MyDoCountChar As String = Str(MyDoCount)
MessageBox.Show("用户密码或用户名称有错,还有" + MyDoCountChar + "次机会!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
Me.用户密码TextBox.Text = ""
Me.用户名称TextBox.Text = ""
Else
退出系统Button_Click(Nothing, Nothing)
End If
End If
If (MyConnection.State = ConnectionState.Open) Then
MyConnection.Close()
End If
End Sub
Private Sub 退出系统Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出系统Button.Click
Dim MyDlg As MainForm = Me.Owner
MyDlg.Close()
End Sub
现需要再定义一全局变量,其值等于用户登录时输入的用户名。