1

我正在创建一个 vb 应用程序,在考试期间将教师分配到考场。我想根据空房情况将教师分配到房间。到目前为止,我已经能够将数据输入作为 csv 文件,将其存储在访问表中并计算教师和房间的数量。但我不知道如何将教师分配到我存储在数据库不同表中的房间。我该怎么做?请帮助我卡住了。

 Private Sub btnTeachersUpload_Click(sender As System.Object, e As System.EventArgs) Handles btnTeachersUpload.Click

    ODTeachers.ShowDialog()
    fileNameTeachers = ODTeachers.FileName
    lblTeacherPath.Text = fileNameTeachers

    teacher_Name = readFromCsv(teacher_Name, 0, fileNameTeachers)
    department = readFromCsv(department, 1, fileNameTeachers)
    phoneno = readFromCsv(phoneno, 2, fileNameTeachers)
    email_id = readFromCsv(email_id, 3, fileNameTeachers)
    teachers_availability = readFromCsv(teachers_availability, 4, fileNameTeachers)

    addToDb("teachers", "teacher_name", teacher_Name)
    addToDb("teachers", "department", department)
    addToDb("teachers", "email_id", email_id)
    addToDb("teachers", "phone_number", phoneno)
    addToDb("teachers", "availability", teachers_availability)
End Sub

Private Sub btnTeachersCompute_Click(sender As System.Object, e As System.EventArgs) Handles btnTeachersCompute.Click
    connect()
    Dim rs As New ADODB.Recordset
    rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
    rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
    rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
    rs.Open("select * from teachers where `availability` = 'yes'", cn)
    rs.ActiveConnection = Nothing

    no_of_avail_teachers = rs.RecordCount
    txtTeachers.Text = no_of_avail_teachers
    cn.Close()
End Sub

Public Function readFromCsv(arr() As String, ByVal index As Integer, ByVal filename As String)
    Using ioReader As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)

        ioReader.TextFieldType = FileIO.FieldType.Delimited
        ioReader.SetDelimiters(",")

        While Not ioReader.EndOfData

            Dim arrCurrentRow As String() = ioReader.ReadFields()

            If arr Is Nothing Then

                ReDim Preserve arr(0)

                arr(0) = arrCurrentRow(index)

            Else

                ReDim Preserve arr(arr.Length)

                arr(arr.Length - 1) = arrCurrentRow(index)
            End If
        End While
    End Using
    Return arr
End Function

Public Sub addToDb(ByVal tablename As String, ByVal fieldname As String, ByVal arr() As String)
    Dim strSql As String
    connect()

    Dim rs As New ADODB.Recordset
    rs.CursorLocation = ADODB.CursorLocationEnum.adUseClient
    rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
    rs.LockType = ADODB.LockTypeEnum.adLockBatchOptimistic
    rs.Open("select * from " & tablename & "", cn)
    rs.ActiveConnection = Nothing

    For i = 0 To arr.Length - 1 Step 1
        strSql = "INSERT INTO " & tablename & " (" & fieldname & ") VALUES ('" & arr(i) & "')"
        cn.Execute(strSql)
    Next
    cn.Close()
End Sub

这是我到目前为止所做的。我已经发布了其中的一部分。我接受一个 excel 表作为输入并将其存储在数据库中。

我需要帮助分配老师到房间。房间部分的代码是相似的。需要将可用的教师分配到可用的房间。

我的教师数据库看起来像这样

teachers_name    availability
 vagarth             yes
 gaurav              no
 nymisha             yes

我的房间数据库看起来像这样

rooms            availability
S203                 yes
H405                 no
S304                 yes
4

0 回答 0