VB.NET建议针对此功能优化我的代码

时间:2019-04-19 05:26:46

标签: vb.net visual-studio visual-studio-2010 optimization query-optimization

您能建议我做什么来优化我的代码。 如您在图片中所看到的,我在一个文本框中显示每个学科的所有学生成绩,共有51个文本框。

enter image description here

在我的数据库中,我有sub_id 1到51,并且我对某些学生的sub_id 1到51重复了相同的查询51次。 这正是我的代码

query = "SELECT grade " & _
       " FROM student_subject" & _
       " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
       " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = 1 AND enrolled = 1 "

    cmd = New MySqlCommand(query, myconn)
    dr = cmd.ExecuteReader

    If dr.Read Then
        textbox_Sub1.Text = dr.Item("grade").ToString
        dr.Close()
    End If

    query = "SELECT grade " & _
       " FROM student_subject" & _
       " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
       " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = 2 AND enrolled = 1 "

    cmd = New MySqlCommand(query, myconn)
    dr = cmd.ExecuteReader

    If dr.Read Then
        textbox_Sub2.Text = dr.Item("grade").ToString
        dr.Close()
    End If

    query = "SELECT grade " & _
      " FROM student_subject" & _
      " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
      " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = 3 AND enrolled = 1 "

    cmd = New MySqlCommand(query, myconn)
    dr = cmd.ExecuteReader

    If dr.Read Then
        textbox_Sub3.Text = dr.Item("grade").ToString
        dr.Close()
    End If

    query = "SELECT grade " & _
      " FROM student_subject" & _
      " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
      " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = 4 AND enrolled = 1 "

    cmd = New MySqlCommand(query, myconn)
    dr = cmd.ExecuteReader

    If dr.Read Then
        textbox_Sub4.Text = dr.Item("grade").ToString
        dr.Close()
    End If

编辑更新:

'** DECLARTING ALL VARIABLES NEEDED
Public GetStudentNum As String
Dim connstring As String = "Data Source=localhost;Database=csais;User ID=root;Password=;"
Dim myconn As MySqlConnection = New MySqlConnection(connstring)
Dim cmd As MySqlCommand
Dim dr As MySqlDataReader
Dim query


'**START FORM LOAD
Private Sub flow1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    GetStudentNum = enrollStd.tempStudentNum
    myconn.Open()

    For i As Integer = 1 To 51
        query = "SELECT grade " & _
        " FROM student_subject" & _
        " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
        " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = " & i.ToString() & " AND enrolled = 1 "

        cmd = New MySqlCommand(query, myconn)
        dr = cmd.ExecuteReader

        If dr.Read Then
            Dim tb As TextBox = CType(Me.Controls.Find("textbox_Sub" & i.ToString(), True)(0), TextBox)
            tb.Text = dr.Item("grade").ToString
            dr.Close()
        End If
    Next

    myconn.Close()

1 个答案:

答案 0 :(得分:1)

尝试为此使用循环:

For i As Integer = 1 to 51
    query = "SELECT grade " & _
   " FROM student_subject" & _
   " INNER JOIN subject_bsit ON subject_bsit.subject_id = student_subject.sub_id" & _
   " where student_subject.student_id='" & GetStudentNum & "' AND sub_id = " & i.ToString() & " AND enrolled = 1 "

    cmd = New MySqlCommand(query, myconn)
    dr = cmd.ExecuteReader

    If dr.Read Then
        Dim tb As TextBox = CType(Me.Controls.Find("textbox_Sub" & i.ToString(), True)(0), TextBox)
        tb.Text = dr.Item("grade").ToString
        dr.Close()
    End If
Next