[英]vb.net setting data source to combobox
i want to set a data source to my combobox when i run there s no error but it keeps showing zeros in the combobox我想在我运行时为我的组合框设置一个数据源,但没有错误,但它一直在组合框中显示零
Dim cnx As New MySqlConnection("datasource=localhost;database=bdgeststock;userid=root;password=")
Dim cmd As MySqlCommand = cnx.CreateCommand
Dim da As MySqlDataAdapter
Dim ds As New DataSet
If ConnectionState.Open Then
cnx.Close()
End If
cnx.Open()
cmd.CommandText = "SELECT idf,(prenom + ' ' + nom) AS NAME FROM fournisseur "
da = New MySqlDataAdapter(cmd)
cnx.Close()
da.Fill(ds)
da.Dispose()
ComboBox1.DataSource = ds.Tables(0)
ComboBox1.ValueMember = "idf"
ComboBox1.DisplayMember = "NAME"
For ComboBox data source you probably don't need heavy Data Set or DataTable - collection of plain object will do the job.对于 ComboBox 数据源,您可能不需要繁重的数据集或数据表 - 普通对象的集合就可以完成这项工作。
Another approach would be to move representation logic to the vb.net code and leave sql server to do persistence logic only.另一种方法是将表示逻辑移至 vb.net 代码,而让 sql server 仅执行持久性逻辑。
Public Class Fournisseur
Public ReadOnly Property Id As Integer
Public ReadOnly Property Name As String
Public Sub New(id As Integer, prenom As String, nom As String)
Id = id
Name = $"{pronom} {nom}".Trim()
End Sub
End Class
You can create dedicated function to load data您可以创建专用函数来加载数据
Private Function LoadItems() As List(Of Fournisseur)
Dim query = "SELECT idf, prenom, nom FROM fournisseur"
Using connection As New MySqlConnection(connectionString)
Using command As New MySqlCommand(query, connection)
connection.Open()
Dim items = new List(Of Fournisseur)()
Using reader AS MySqlDataReader = command.ExecuteReader()
While reader.Read()
Dim item As New Fournisseur(
reader.GetInt32(0),
reader.GetString(1),
reader.GetString(2)
)
items.Add(item)
End While
End Using
Return items
End Using
End Using
End Function
Then usage will look pretty simple然后使用看起来很简单
ComboBox1.ValueMember = "Id"
ComboBox1.DisplayMember = "Name"
ComboBox1.DataSource = LoadItems()
I think the problem is in your sql, and mysql is performing some sort of numeric addition on prenom plus nom and producing 0我认为问题出在您的 sql 中,而 mysql 正在对 prenom 和 nom 执行某种数字加法并产生 0
Try尝试
CONCAT(prenom, ' ', nom) as name
In your sql instead.在你的 sql 中。 I prefer using concat in most RDBMS for concatenating strings because is is more consistent with its behaviour on NULLs - in sqlserver, using the concat operator of plus on something like 'a' + null
results in NULL but in oracle 'a' || null
我更喜欢在大多数 RDBMS 中使用 concat 来连接字符串,因为它更符合它在 NULL 上的行为 - 在 sqlserver 中,在诸如'a' + null
类'a' + null
东西上使用加号的 concat 运算符会导致 NULL 但在 oracle 'a' || null
'a' || null
is a
- in both the CONCAT behaviour is consistent 'a' || null
是a
- 在两个 CONCAT 行为中是一致的
Here's a full code with all my recommendations:这是包含我所有建议的完整代码:
Dim cnstr = "datasource=localhost;database=bdgeststock;userid=root;password="
Dim cmd = "SELECT idf, CONCAT(prenom, ' ', nom) AS nom FROM fournisseur "
Using da As New MySqlDataAdapter(cmd, cnstr)
Dim dt As New DataTable
da.Fill(dt)
ComboBox1.DataSource = dt
ComboBox1.ValueMember = "idf"
ComboBox1.DisplayMember = "nom"
End Using
Tips:提示:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.