VB.NET 针对此功能优化我的代码的建议
VB.NET Suggestion for optimizing my code for this function
你能建议我如何优化我的代码吗?
正如您在图片中看到的,我在一个文本框中显示了每个科目的所有学生成绩,共有 51 个文本框。
在我的数据库中,我有 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()
为此尝试使用循环:
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
你能建议我如何优化我的代码吗? 正如您在图片中看到的,我在一个文本框中显示了每个科目的所有学生成绩,共有 51 个文本框。
在我的数据库中,我有 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()
为此尝试使用循环:
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