combobox控件如何绑定SQL数据库
以vb.net2008中,如何将combobox绑定的数据库中的某个字段?
因为我之前是用vb6.0的,vb.net2008第一次接触。
所以我的代码还是很接近vb6.0的
我的代码是:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OpenDb()
yu.Open("select department_name from department", cnn, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockOptimistic)
If yu.RecordCount > 0 Then
Do While yu.EOF = False
ComboBox1.Items.Add(yu.Fields(0))
yu.MoveNext()
Loop
End If
yu.Close()
CloseDb()
End Sub
End Class
按下F5运行,居然也没有出错,但是combobox的下列表框中的值是:
System.__ComObject
请高手们指教一个,这是什么原因,在vb.net2008如何才能将控件与数据库绑定?
vb6.0与vb.net2008又有多大的区别呢?
说明:其中的Department为部门表,Department_name为该表中的部门名称字段。
[最优解释]
Public Sub FillDataToCombobox(ByVal objTableName As String, ByVal objColumnName As String, ByVal objCombobox As ComboBox)
Dim objDataSet As New DataSet
Using objData As New WDABase
Try
objDataSet.Clear()
objData.SQL = "Select DISTINCT " & objColumnName & " from " & objTableName & " where " & objColumnName & " is not null"
objData.OpenConnection()
objData.FillDataSet(objDataSet, objTableName)
objCombobox.DataSource = objDataSet.Tables(objTableName)
objCombobox.DisplayMember = objColumnName
objCombobox.AutoCompleteSource = AutoCompleteSource.ListItems
objCombobox.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest
objCombobox.Text = ""
Catch ex As Exception
MsgBox("错误信息:" + ex.ToString, MsgBoxStyle.Critical, "System")
Finally
End Try
End Using