记录超时后的超时时间-RFID学生大门通行证ID系统

时间:2018-11-28 22:20:58

标签: vb.net

我正在使用VB.Net和MySql。 我有3个名为RFIDTag,TimeIn和TimeOut的表。

当我点击一次时,它将记录到TimeIn中,然后当我点击两次时,时间必须记录到TimeOut中。

我的工作已经记录了TimeIn,这是我记录TimeOut的逻辑。

If (Count from the table where RFIDTag matches the textbox from vb.net) > 1 then
    If TimeOut = ""
          records Time into the TimeOut
    Else
          INSERT new record NEW row of TimeIn
Else
    INSERT new record NEW TimeIn

这是我记录TimeIn的代码。

    Using sqlcommand As New MySqlCommand
        With sqlcommand
            .CommandText = "INSERT INTO userrecord (RFIDTag, TimeIn) VALUES (@RFIDTag, @TimeIn)"
            .Connection = con
            .CommandType = CommandType.Text
            .Parameters.AddWithValue("@RFIDTag", TextBoxTag.Text)
            .Parameters.AddWithValue("@TimeIn", TimeIn)
        End With

        sqlcommand.ExecuteNonQuery()
    End Using

    sqlreader = query.ExecuteReader
    sqlreader.Read()

    LabelName.Text = sqlreader.Item("FullName")
    LabelYearCourse.Text = sqlreader.Item("YearCourse")
    LabelIDNum.Text = sqlreader.Item("IDNumber")
    sqlreader.Close()


    TextBoxTag.Text = ""
    LabelStatus.Text = "IN"
    con.Close()
End If

这是我的学校项目,我只是使用VB.Net的那个,所以我不能问我的同学。我也做了很多研究,但我不能再接受了。我将在3天内展示该系统,但尚未完成=(我希望有人可以向我提供帮助。

1 个答案:

答案 0 :(得分:0)

数据库中有2个表呢。表RFIDTag,其中IDNumber列为主键,然后为FullName和YearCourse。表TimeInOut,其中IDNumber列为外键,然后是TimeIn和TimeOut。

现在在登录时保留本地学生字典。

Private LoggedInDictionary As Dictionary(Of Integer,DateTime)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoggedInDictionary = New Dictionary(Of Integer, DateTime)
End Sub

根据学生是否在LoggedInDictionary中,从“记录”按钮中调用RecordLogIn或RecordLogOut。

Private Sub btnRecord_Click(sender As Object, e As EventArgs) Handles btnRecord.Click
        Dim key As Integer = CInt(TextBoxTag.Text)
        Dim value As DateTime
        'If the student is not in the dictionary
        If Not LoggedInDictionary.TryGetValue(key, value) Then
            RecordLogIn(key)
        Else
            RecordLogOut(key, value)
        End If
        ListBox1.Items.Clear()
        DisplayLoggedInStudents()
    End Sub

Private Sub RecordLogIn(StudentId As Integer)
        Dim ID As Integer = StudentId
        Dim LogInTime As DateTime = DateTime.Now
        LoggedInDictionary.Add(ID, LogInTime)
    End Sub

Private Sub RecordLogOut(ID As Integer, TimeIn As DateTime)
        Dim InsertQuery As String = "Insert into TimeInOut (IDNumber, TimeIn, TimeOut) Values (@ID, @TimeIn, @TimeOut);"
        Using con As New MySqlConnection("Your connection string")
            Using cmd As New MySqlCommand(InsertQuery, con)
                cmd.Parameters.AddWithValue("@ID", ID)
                cmd.Parameters.AddWithValue("@TimeIn", TimeIn)
                cmd.Parameters.AddWithValue("@TimeOut", DateTime.Now)
                con.Open()
                cmd.ExecuteNonQuery()
            End Using
        End Using
        LoggedInDictionary.Remove(ID)
    End Sub

在btnRecord子标题的末尾,已登录学生的显示已更新。

Private Sub DisplayLoggedInStudents()
        For Each item As KeyValuePair(Of Integer, DateTime) In LoggedInDictionary
            Dim LoggedInStudents As String = $"Student Name: {GetStudentNameByID(item.Key)}, Student ID: {item.Key} Logged In Time: {item.Value}"
            ListBox1.Items.Add(LoggedInStudents)
        Next
    End Sub

显示使用GetStudentNameByID。

Private Function GetStudentNameByID(StudentID As Integer) As String
        Dim StudentName As String = ""
        Using con As New MySqlConnection("Your Connection String")
            Using cmd As New MySqlCommand("Select FullName From RFIDTag Where IDNumber = @ID;")
                cmd.Parameters.Add(@ID, StudentID)
                con.Open()
                StudentName = cmd.ExecuteScalar.ToString
            End Using
        End Using
        Return StudentName
    End Function

您可以看到已登录学生的列表,并在一天结束时将其注销。有些边缘情况不适用于此系统。此代码未经测试,因为没有数据库。