vb.net 用来自 MySQL 的数据填充组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21724594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate combobox with data from MySQL
提问by Livaren
I'm trying to populate my combobox with data from a table in mysql, here is my code, I can't see what the issue is. My combobox doesn't return any data, it is just blank.
我正在尝试用 mysql 中的表中的数据填充我的组合框,这是我的代码,我看不出问题是什么。我的组合框不返回任何数据,它只是空白。
Private Sub cbxCompanyName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxCompanyName.SelectedIndexChanged
Dim con As New MySqlConnection
Dim constr As String = "Server=localhost;database=ba-solutions;user id=root;password="
Try
con.ConnectionString = constr
con.Open()
Catch ex As SqlException
MsgBox(ex.Message)
End Try
Dim StrSql As String = "SELECT Company Name FROM client_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("Client_details")
da.Fill(dt)
If dt.Rows.Count > 0 Then
cbxCompanyName.DataSource = dt
cbxCompanyName.DisplayMember = "Company Name" 'What is displayed
End If
End Sub
回答by ekad
Try this
尝试这个
If dt.Rows.Count > 0 Then
cbxCompanyName.DataSource = dt
cbxCompanyName.DisplayMember = "Company Name"
cbxCompanyName.ValueMember = "Company Name"
End If
回答by Steve
You sql command text is wrong. When you have spaces in the column names or table names you should enclose the name in a special character that delimits the start and the end of the column name.
你的sql命令文本是错误的。当列名或表名中有空格时,您应该将名称括在一个特殊字符中,该字符分隔列名的开头和结尾。
Dim StrSql As String = "SELECT `Company Name` FROM client_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim da As MySqlDataAdapter = New MySqlDataAdapter(cmd)
Dim dt As New DataTable("Client_details")
For MySql this special character is the backtick (keep pressed ALTand press 096on the numeric keyboard).
对于 MySql,这个特殊字符是反引号(按住ALT并按下096数字键盘)。
Of course it would be much better to avoid these spaces at all.
If it is not too late I suggest to change that column name removing the space
当然,完全避免这些空间会好得多。
如果现在还不算太晚,我建议更改该列名称以删除空格
回答by Jegadeesh
Private Sub cbxCompanyName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxCompanyName.SelectedIndexChanged Dim con As New MySqlConnection Dim constr As String = "Server=localhost;database=ba-solutions;user id=root;password=" Try con.ConnectionString = constr con.Open() Catch ex As SqlException MsgBox(ex.Message) End Try
Private Sub cbxCompanyName_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) 处理 cbxCompanyName.SelectedIndexChanged Dim con As New MySqlConnection Dim constr As String = "Server=localhost;database=ba-solutions;user id=root;password=" Try con.ConnectionString = constr con.Open() Catch ex As SqlException MsgBox(ex.Message) End Try
Dim StrSql As String = "SELECT Company Name FROM client_details"
Dim cmd As New MySqlCommand(StrSql, objconnection)
Dim dr As OleDbDataReader=cmd.ExecuteReader
Dim dr As OleDbDataReader=cmd.ExecuteReader
While dr.Read
cbxCompanyName.items.add(dr.item(0))
End While
dr.close()
End Sub
结束子
回答by Thurman Jenner
All of the above is correct when placing into a combobox, but one thing is omitted. In order to immediately access that list the starting index must be set for the selected items, or you will return the contents of 'System.Data.DataRowView' when ever you go to collect the contents of ComboBox1.Text.
放入组合框时,以上所有内容都是正确的,但省略了一件事情。为了立即访问该列表,必须为所选项目设置起始索引,否则您将在收集 ComboBox1.Text 的内容时返回“System.Data.DataRowView”的内容。
da.SelectCommand = cmd
ds = New DataSet
Dim dt As New DataTable
da.Fill(ds, Table)
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.DisplayMember = "imgURL"
conn.Close()
da.Dispose()
conn.Dispose()
ComboBox1.SelectionStart = 0 'This is what is missing from above.
Dim current_imgURL As String = "http://10.1.1.10/photos/" & ComboBox1.Text
PictureBox1.Image = New Bitmap(New IO.MemoryStream(New WebClient().DownloadData(current_imgURL)))
This example loads a combobox with image file names and pulls them from a server location. I wanted to preload the first image from the combobox right after I filled it from a MySql and ran into this issue because the index for selection is triggered only when the user clicks the combobox, not when data is loaded.
此示例加载带有图像文件名的组合框,并从服务器位置拉取它们。我想在从 MySql 填充后立即从组合框中预加载第一个图像并遇到此问题,因为选择索引仅在用户单击组合框时触发,而不是在加载数据时触发。
回答by Jegadeesh
Try This..!
尝试这个..!
If dt.Rows.Count > 0 Then
如果 dt.Rows.Count > 0 那么
cbxCompanyName.DataSource = dt
**cbxCompanyName.DataBind();**
End If
回答by Rahul soni
Try this Code i hope that it should work
试试这个代码,我希望它可以工作
Dim con As New MySqlConnection("connection string")
Dim da As New MySqlDataAdapter("SELECT * FROM tableName", con)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DisplayMember = "fieldName to show in ComboBox"enter code here
ComboBox1.DataSource = dt
TextBox1.DataBindings.Add("Text", dt, "fieldName to show in textbox")

