我有一个包含以下列的数据库:
RegNumber | LastName | FirstName | OOPVB | DataComms | ModelsDB | MAB | OB | CF
我想填充包含OOPVB,DataComms,ModelsDB,MAB,OB
和CF
列的ComboBox。
我只使用此代码设法为单个列执行此操作:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
mysqlConn = New MySqlConnection
mysqlConn.ConnectionString =
"server=127.0.0.1;userid=root;database=studentsresults1"
Dim SDA As New MySqlDataAdapter
Dim dbDataSet As New DataTable
Dim bSource As New BindingSource
Try
mysqlConn.Open()
Dim Query As String
Query = "select * from studentsresults1.courses"
SQLcmd = New MySqlCommand(Query, mysqlConn)
SDA.SelectCommand = SQLcmd
SDA.Fill(dbDataSet)
bSource.DataSource = dbDataSet
DataGridView1.DataSource = bSource
SDA.Update(dbDataSet)
mysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
mysqlConn.Dispose()
End Try
End Sub
如何为多列执行此操作?请帮助
答案 0 :(得分:0)
如果我理解得对,你想用表Marks所说的列中的所有数据填充组合,对吧?
一种方式(非常丑陋但功能齐全)是对其余列重复该操作,为另一列执行另一个SQL并以此方式添加结果。
更简洁的方法是循环所有细胞:
Private Sub GetCourses()
SQL.ExecQuery("SELECT OOPVB,DataComms,ModelsDB,MAB,OB, CF FROM marks")
If SQL.RecordCount > 0 Then
For Each r As DataRow In SQL.SQLDS.Tables(0).Rows
For Each c in r.ItemArray
ComboBox1.Items.Add(c.Value)
Next
Next
ComboBox1.SelectedIndex = 0
ElseIf SQL.Exception <> "" Then
MsgBox(SQL.Exception)
End If
End Sub