如何在 VB.NET 中创建日志历史并将其保存到 mysql workbench 数据库中?

How to create a log history in VB.NET and saves it into mysql workbench database?

我对vb.net知之甚少,我正在努力了解它。我已经尝试使用下面的登录按钮代码来保存登录日期和时间,并且可以正常工作。但是,我不确定要为注销按钮输入什么代码。请帮我解决这个问题,谢谢! :)

Private Sub Button1_LoggedIn(sender As Object, e As EventArgs) Handles Button1.Click
    Dim command As New MySqlCommand("SELECT `StudentId`, `Account Password` FROM `usersaccount` WHERE `StudentId` = @username AND `Account Password` = @password", connection)
    command.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username.Text
    command.Parameters.Add("@password", MySqlDbType.VarChar).Value = Pass.Text
    Dim adapter As New MySqlDataAdapter(command)
    Dim table As New DataTable()

    adapter.Fill(table)

    If table.Rows.Count = 0 Then

        MessageBox.Show("Invalid Username Or Password")

    Else

        Using con As New MySqlConnection("server=localhost;username=root;password=;database=logsrecord"),
                cmd As New MySqlCommand("Insert into loghistory.logsrecord (Username, Date, LoggedIn) Values (@Username, @Date, @LoggedIn);", con)
            cmd.Parameters.Add("@Username", MySqlDbType.VarChar).Value = Username.Text
            cmd.Parameters.Add("@Date", MySqlDbType.VarChar).Value = DateTime.Now.ToString("MMM-dd-yyyy")
            cmd.Parameters.Add("@LoggedIn", MySqlDbType.VarChar).Value = TimeOfDay.ToString("hh:mm:ss")
            con.Open()
            cmd.ExecuteNonQuery()
        End Using
        Form3.Show()
        Me.Hide()


    End If

End Sub

我将您的按钮名称更改为更具描述性的名称。

您为第二个数据库连接使用了 Using 块,但不是第一个。如果对象在文档中列出了 Dispose 方法,则您希望养成始终使用 Using 块的习惯。

你不需要下拉Account Password。这对您的代码来说不是必需的,而且线路上的密码越少越好。实际上,您应该永远不要 将密码存储为纯文本。它们应该总是 加盐和散列。

MySql是关系型数据库。表可以通过主 Key/Foreign 键关系相互关联。您可以使用 usersaccount table 中的主键作为 logsrecord table 中的外键。

Private OPConStr As String = "Your connection string."

Private Sub btnLogIn_Click(sender As Object, e As EventArgs) Handles btnLogIn.Click
    Dim ID As Integer
    Using connection As New MySqlConnection(OPConStr),
            cmd As New MySqlCommand("SELECT `StudentId` FROM `usersaccount` WHERE `StudentId` = @username AND `Account Password` = @password", connection)
        cmd.Parameters.Add("@username", MySqlDbType.VarChar).Value = Username.Text
        cmd.Parameters.Add("@password", MySqlDbType.VarChar).Value = Pass.Text
        connection.Open()
        ID = CInt(cmd.ExecuteScalar())
    End Using
    If ID = 0 Then
        MessageBox.Show("Invalid Username Or Password")
        Exit Sub
    End If
    Using con As New MySqlConnection("server=localhost;username=root;password=07292021;database=logsrecord"),
                cmd As New MySqlCommand("Insert into loghistory.logsrecord (StudentID, DateIn, Action) Values (@ID, @Date, @Action);", con)
        cmd.Parameters.Add("@ID", MySqlDbType.VarChar).Value = ID
        cmd.Parameters.Add("@Date", MySqlDbType.DateTime).Value = Now()
        cmd.Parameters.Add("@Action", MySqlDbType.Int32).Value = 1
        con.Open()
        cmd.ExecuteNonQuery()
    End Using
    Form3.Show()
    Hide()
End Sub

编辑

要通过注销更新登录记录DateTime,您需要登录记录的主键。假设您有一个用于主键 logsrecord table 的自动递增字段。您可以在插入查询后使用 Select LAST_INSERT_ID();https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id 然后 ExecuteScalar 将 return 为主键。

您需要将此值存储在您的应用程序中,直到用户注销。我假设一个模块带有一个名为 LogInRecordID.

的变量

修改后的代码可能如下所示。

    Dim strSql = "Insert into loghistory.logsrecord (StudentID, DateIn, Action) Values (@ID, @Date, @Action);
                  Select LAST_INSERT_ID();"
    Dim InsertRecordID As Integer
    Using con As New MySqlConnection("server=localhost;username=root;password=07292021;database=logsrecord"),
            cmd As New MySqlCommand(strSql, con)
        cmd.Parameters.Add("@ID", MySqlDbType.VarChar).Value = ID
        cmd.Parameters.Add("@Date", MySqlDbType.DateTime).Value = Now()
        cmd.Parameters.Add("@Action", MySqlDbType.Int32).Value = 1
        con.Open()
        InsertRecordID = CInt(cmd.ExecuteScalar())
    End Using
    SomeModule.LogInRecordID = InsertRecordID

当用户注销时调用代码如下。用户可以在不正确注销的情况下关闭应用程序。您可能希望从应用程序事件中调用它。 https://docs.microsoft.com/en-us/dotnet/visual-basic/developing-apps/programming/log-info/how-to-log-messages-when-the-application-starts-or-shuts-down

Private Sub LogOut()
    Dim strSql = "Update loghistory.logsrecord Set DateOut = @Date Where ID = @ID;"
    Using cn As New MySqlConnection(OPConStr),
            cmd As New MySqlCommand(strSql, cn)
        cmd.Parameters.Add("@Date", MySqlDbType.DateTime).Value = Now
        cmd.Parameters.Add("@ID", MySqlDbType.Int32).Value = SomeModule.LogInRecordId
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub