将SQL数据插入列表框 [英] Insert sql data into listbox
本文介绍了将SQL数据插入列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行此代码时,我的listbox
为空.从SQL获取数据并将其放入listbox
的最佳方法是什么?提交表单后,我想使用CustomreID
值数据存储到另一个表中,并认为使用索引将是最佳解决方案.
When I run this code my listbox
is empty. What is the best way to get data from SQL and into a listbox
? When the form is submitted I want to use the CustomreID
value data to store into another table and thought using the index would be the best solution.
sSQL = "SELECT CustomerID, Company from Customers Order by Company ASC"
cmd = New SqlCommand(sSQL, moConn)
rs = cmd.ExecuteReader()
While rs.Read
lsbDestination.Items.Insert(CInt(rs("CustomerID")), rs("Company"))
End While
推荐答案
您可以使用ListBox的DataSource
属性轻松地将数据绑定到ListBox.尝试这样的事情(未经测试):
You can easily bind data to a ListBox using the DataSource
property of the ListBox. Try something like this (untested):
Dim adapter As New SqlDataAdapter(cmd)
Dim ds As New DataSet
adapter.Fill(ds)
lsbDestination.DataTextField = "Company"
lsbDestination.DataValueField = "CustomerId"
lsbDestination.DataSource = ds.Tables(0)
lsbDestination.DataBind()
祝你好运.
这篇关于将SQL数据插入列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文