[英]Merging Functions into one statement on VB.Net
我正在制作一個學生檔案系統,我在 VB 上有一個表格,可以將數據從 MySQL 檢索到復選框,當列上有“1”時,復選框就會打勾。
我在每個復選框上執行以下功能和查詢。 有沒有辦法將它合並到一個查詢中?
Sub CheckboxAccess1()
Using con As New MySqlConnection(str)
Try
con.Open()
Dim sql As String = "SELECT access1 FROM tblusers WHERE userid = @userid AND access1 LIKE '1'"
com = New MySqlCommand(sql, con)
com.Parameters.AddWithValue("userID", TextBox2.Text)
Dim reader As MySqlDataReader
reader = com.ExecuteReader
If reader.Read() Then
CheckBox2.Checked = True
Else
CheckBox2.Checked = False
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
con.Dispose()
End Try
End Using
End Sub
Sub CheckboxAccess2()
Using con As New MySqlConnection(str)
Try
con.Open()
Dim sql As String = "SELECT access2 FROM tblusers WHERE userid = @userid AND access2 LIKE '1'"
com = New MySqlCommand(sql, con)
com.Parameters.AddWithValue("userID", TextBox2.Text)
Dim reader As MySqlDataReader
reader = com.ExecuteReader
If reader.Read() Then
CheckBox3.Checked = True
Else
CheckBox3.Checked = False
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
con.Dispose()
End Try
End Using
End Sub
Sub CheckboxAccess()
Using con As New MySqlConnection(str)
Try
con.Open()
Dim sql As String = "SELECT access1, access2 FROM tblusers WHERE userid = @userid"
com = New MySqlCommand(sql, con)
com.Parameters.AddWithValue("userID", TextBox2.Text)
Dim reader As MySqlDataReader
reader = com.ExecuteReader
reader.Read()
CheckBox2.Checked = (reader.GetString("access1") = "1")
CheckBox3.Checked = (reader.GetString("access2") = "1")
Catch ex As Exception
MessageBox.Show(ex.ToString)
Finally
con.Dispose()
End Try
End Using
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.