这是记录所有状态(IN / OUT)的事件表:
+--------------------------------------------------+-----------------+
| Event_ID | User_BannerID | Group_ID | Timestamp | Status | Creator|
+----------+----------------+----------+-----------+--------+--------+
| | | | | | |
| | | | | | |
+----------+----------------+----------+-----------+--------+--------+
所以我正在使用vb.net和mysql制作一个签名输入程序。我想知道是否有一个代码可以放在某个地方就像用户打开应用程序他/她必须在他们可以注销之前签名,如果他们打开应用程序并且他们已经登录,他们又不能再签名了必须先注销才能再次退出。
Imports MySql.Data.MySqlClient
Imports System.Data
Public Class frmMain
Private myConnString As String
Private myUserID As String
Public WriteOnly Property connectionString() As String
Set(ByVal value As String)
myConnString = value
End Set
End Property
Public WriteOnly Property UserID() As String
Set(ByVal value As String)
myUserID = value
End Set
End Property
Private Sub refreshStatus(ByRef statusView As DataGridView)
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT CONCAT(u.lastname, ', ', u.firstname) AS Name, ug.group_name AS Class, DATE_FORMAT(e.timestamp,'%b %d %Y - %r')AS DateTime, e.status AS Status " _
& "FROM event e, user u, user_group ug " _
& "WHERE(e.user_bannerid = u.user_bannerid) " _
& "AND e.group_id = ug.group_id " _
& "AND ug.user_bannerid = ?userID " _
& "AND event_id IN " _
& "( " _
& "Select MAX(e.event_id) " _
& "FROM event e " _
& "GROUP BY e.user_bannerid " _
& ") " _
& "ORDER BY datetime"
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myCommand.Parameters.Add("?userID", myUserID)
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
dgvStatus.DataSource = myData
dgvStatus.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboStatus.Items.Add("In")
cboStatus.Items.Add("Out")
cboStatus.SelectedIndex = 0
dgvStatus.ReadOnly = True
refreshStatus(dgvStatus)
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As New DataTable
Dim SQL As String
SQL = "SELECT ug.group_id, ug.group_name " _
& "FROM attendance.user_group ug " _
& "WHERE user_bannerid = ?userID and level_id is NULL "
conn.ConnectionString = myConnString
Try
conn.Open()
Try
myCommand.Connection = conn
myCommand.CommandText = SQL
myCommand.Parameters.Add("?userID", myUserID)
myAdapter.SelectCommand = myCommand
myAdapter.Fill(myData)
cboClass.DataSource = myData
cboClass.DisplayMember = "group_name"
cboClass.ValueMember = "group_id"
Catch myerror As MySqlException
MsgBox("There was an error reading from the database: " & myerror.Message)
End Try
Catch myerror As MySqlException
MessageBox.Show("Error connecting to the database: " & myerror.Message)
Finally
If conn.State <> ConnectionState.Closed Then conn.Close()
End Try
End Sub
Private Sub cmdUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdUpdate.Click
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
conn.ConnectionString = myConnString
myCommand.Connection = conn
myCommand.CommandText = "INSERT INTO event(user_bannerid, group_id, timestamp, status, creator)" _
& "VALUES(?UserID, ?GroupID, NOW(), ?Status, ?Creator)"
myCommand.Parameters.Add("?UserID", myUserID)
myCommand.Parameters.Add("?GroupID", cboClass.SelectedValue)
myCommand.Parameters.Add("?Status", cboStatus.SelectedItem)
myCommand.Parameters.Add("?Creator", myUserID)
Try
conn.Open()
myCommand.ExecuteNonQuery()
MessageBox.Show("Status Successfully Updated")
Catch myerror As MySqlException
MsgBox("There was an error updating the database: " & myerror.Message)
End Try
refreshStatus(dgvStatus)
End Sub
Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
Dim oForm As frmLogin
oForm = New frmLogin()
frmLogin.Show()
oForm = Nothing
Me.Hide()
End Sub
Private Sub frmLogin_Click(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
Dim oForm As frmLogin
oForm = New frmLogin()
frmLogin.Show()
oForm = Nothing
Me.Hide()
End Sub
End Class
答案 0 :(得分:2)
为什么不在用户再次登录之前自动签名用户?您可以告诉他们(通过消息框或其他内容),如果您发现他们之前已登录过,则会自动退出他们之前的会话。
<强>更新强>
在审核您的问题和代码时,我认为您依靠活动日志来提供用户状态,从而使您的生活变得非常困难。活动日志应仅用于记录离散活动,而不是用于确定用户的状态。
我建议您将状态字段添加到用户表。这将始终存储或移出用户的当前状态。
当用户打开您的应用程序时,您将进行简单的检查:用户的状态是什么?如果用户已登录,请向用户显示“注销”按钮。如果用户未登录,则向他们显示“注销”按钮。不要让他们从下拉菜单中选择他们的状态是什么,因为我可以保证他们会弄乱一些东西。
如果用户已登录但又想再次登录,则可以按退出按钮。您将使用新状态更新用户表并记录活动,然后禁用“注销”按钮并启用“登录”按钮。当他们按下登录按钮时,您将更新用户状态并记录新活动。
作为一个选项,如果用户在启动应用程序时当前已登录,则可以在上次登录时显示它们(来自活动日志或作为用户记录中的单独字段),这样就不会混淆关于为什么他们被提供注销按钮。
使用特定代码进行更新
Private Const STATUS_IN As String = "In"
Private Const STATUS_OUT As String = "Out"
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Disable the buttons to start
btnLogin.Enabled = False
btnLogout.Enabled = False
Dim sCurrentStatus As String
' Get the user's current status
sCurrentStatus = GetUserStatus()
' And update the buttons based on this status change
Call UpdateButtonsForStatusChange(sCurrentStatus)
End Sub
Private Sub UpdateButtonsForStatusChange(sStatus As String)
Dim fLoggedIn As Boolean
' Determine whether or not the user is logged in based on the status
fLoggedIn = sStatus.Equals(STATUS_IN, StringComparison.InvariantCultureIgnoreCase)
' If the user is NOT logged in, enable the login button
btnLogin.Enabled = Not fLoggedIn
' If the user IS logged in, enable the logout button
btnLogout.Enabled = fLoggedIn
End Sub
Private Sub btnLogin_Click(sender As System.Object, e As System.EventArgs) Handles btnLogin.Click
' Indicate that the user is logging in
Call HandleButtonClick(STATUS_IN)
End Sub
Private Sub btnLogout_Click(sender As System.Object, e As System.EventArgs) Handles btnLogout.Click
' Indicate that the user is logging out
Call HandleButtonClick(STATUS_OUT)
End Sub
Private Sub HandleButtonClick(sNewStatus As String)
' Update the database with the user's new status
Call UpdateUserStatus(sNewStatus)
' And update the buttons based on this status change
Call UpdateButtonsForStatusChange(sNewStatus)
End Sub
Private Function GetUserStatus() As String
' ToDo: Add your code to get the user's current status here
End Function
Private Sub UpdateUserStatus(sNewStatus As String)
' ToDo: Add your code to save the user's current status here
End Sub
答案 1 :(得分:0)
好吧,如果你不喜欢competent_tech的答案。 问自己这些问题。
如果您外出,但尚未退出,请退出吗?
如果您还没有登录,可以退出吗?
在任何一种情况下,“sumfin已经走了”。
缺少退出。
退出然后登录,你的用户会发现,如果你让他们跳过篮球,当你刚刚签署并离开那里时,会非常恼火。
另一个是代码中的某个漏洞,它发生的频率是真正的问题,关闭逻辑中的漏洞,也许是某种未处理的异常。在某种情况下,您永远无法保证您的代码能够正常关闭,即使您因为无法成功注销而拒绝关闭,用户仍然可能会让您感到困惑。浪费精力。