我有以下代码:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
MySqlConn = New MySqlConnection
MySqlConn.ConnectionString = "server=localhost;userid=root;password=root;database=test"
Dim READER As MySqlDataReader
Try
MySqlConn.Open()
Dim Query As String
Query = "select * from test.boxinformation where Box_SN='" & ComboBox1.Text & "'"
COMMAND = New MySqlCommand(Query, MySqlConn)
READER = COMMAND.ExecuteReader
ComboBox2.Items.Clear()
While READER.Read
Dim CP = READER.GetString("CP_IP")
Dim PC = READER.GetString("PC_IP")
Dim M1 = READER.GetString("M1_IP")
Dim M2 = READER.GetString("M2_IP")
ComboBox2.Items.Add(CP)
ComboBox2.Items.Add(PC)
ComboBox2.Items.Add(M1)
ComboBox2.Items.Add(M2)
End While
MySqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MySqlConn.Dispose()
End Try
End Sub
基本上,正在发生的事情是我有一个SQL数据库,其中包含特定于组件的IP地址。因此,当用户在ComboBox1中选择Component时,特定于该Component的IP地址将填充在ComboBox2中。
我的数据库设置在一个包含5列的表中。 ComboBox1从名为“Box_SN”的列中提取数据,ComboBox2从名为“CP_IP”,“PC_IP”,“M1_IP”和“M2_IP”的列中提取数据。当用户在ComboBox1中进行选择时,与ComboBox1中所选组件关联的IP地址将显示在ComboBox2中。所以现在有4个IP地址填充在ComboBox2中,这些IP地址特定于ComboBox1中的选定框。所有这一切都按照应有的方式发挥作用。
但我没有在ComboBox2中显示IP地址,而是想显示文本。因此对于“CP_IP”而不是显示其IP地址,我希望它显示“组件A”,而对于“PC_IP”,我希望它显示“组件B”
所以我想我想说的是,我想通过我的数据库将IP地址分配给文本而不仅仅是正在读取的IP。
如果有人能帮助我,我会非常感激。
答案 0 :(得分:1)
在网络上,html B(int x, int y = 0) : A(y)
{
b=x;cout<<b;
cout<<"B Constructor\n";
}
元素包含内容和值的选项。在Windows窗体中,ComboBox元素只有select
。只使用字符串对象是很常见的。
但是
只要Objects
方法产生有意义的东西,您就可以在其中放置任何类型的对象。这些,例如:
.ToString()
然后,还花时间修复大量SQL注入问题,原始方法如下所示:
Public Class ComboItem(Of T)
Public Property Name As String
Public Property Value As T
Public Sub New()
End Sub
Public Sub New(name As String, value As T)
Me.Name = name
Me.Value = value
End Sub
Public Overrides Function ToString() As String {
Return Name
End Function
End Class
现在,当用户在ComboBox 2中进行选择时,他们可以根据“友好”文本做出选择,但您仍然可以访问所需的IP值。
最后,最好将所有数据访问权限移到与UI分开的位置,如下所示:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Query As String = "select * from test.boxinformation where Box_SN= @BoxSN ;"
Using MySqlConn As New MySqlConnection("server=localhost;userid=NOTROOT;password=root;database=test"), _
COMMAND As New MySqlCommand(Query, MySqlConn)
COMMAND.Parameters.Add("@BoxSN", MySqlDbType.VarChar, 15).Value = ComboBox1.Text
MySqlConn.Open()
Using READER As MySqlDataReader = COMMAND.ExecuteReader()
While READER.Read
ComboBox2.Items.Add(New ComboItem(Of String)("Component A", READER.GetString("CP_IP")))
ComboBox2.Items.Add(New ComboItem(Of String)("Component B", READER.GetString("PC_IP")))
'Guessing at the names here, since they aren't in the question
ComboBox2.Items.Add(New ComboItem(Of String)("Machine 1", READER.GetString("M1_IP")))
ComboBox2.Items.Add(New ComboItem(Of String)("Machine 2", READER.GetString("M2_IP")))
End While
READER.Close()
End Using
End Using
End Sub
然后用你原来的方法调用它:
Public Module Data
Private Const ConnectinString As String = "server=localhost;userid=NOTROOT;password=root;database=test"
Public Function GetComponentIPs(BoxSN As String) As String()
Dim Query As String = "select * from test.boxinformation where Box_SN= @BoxSN ;"
Using MySqlConn As New MySqlConnection(ConnectionString), _
COMMAND As New MySqlCommand(Query, MySqlConn)
COMMAND.Parameters.Add("@BoxSN", MySqlDbType.VarChar, 15).Value = BoxSN
MySqlConn.Open()
Dim result(3) As String
Using READER As MySqlDataReader = COMMAND.ExecuteReader()
While READER.Read
'You could also do this with Tuples, or make another custom object (class) to hold this data to keep things strongly-typed
result(0) = READER.GetString("CP_IP")
result(1) = READER.GetString("PC_IP")
result(2) = READER.GetString("M1_IP")
result(3) = READER.GetString("M2_IP")
Return result
End While
End Using
End Using
Return result
End Function
End Module