2013年05月06日

ComboBox组件如何绑定数据库
可以在winform里根据向导完成,但是灵活性较差,下面介绍combobox使用代码绑定显示数据库字段。

       Dim sqlconstr As String = getSqlConstr() '定义并获取SQL连接字符串
        Dim customersTableAdapter As New SqlDataAdapter("Select CAUSE2_ID from BTS_CAUSE_TABLE", sqlconstr)   ’实例化一个SqlDataAdapter
        Dim customerTable As New DataTable() ’实例化一个Datatable
        customersTableAdapter.Fill(customerTable) '调用tableadapter的fill方法,把数据写入customerTable
        ComBoxCause2.DataSource = customerTable  ‘绑定combobox 数据源到customerTable
        ComBoxCause2.DisplayMember = "CAUSE2_ID" ‘设置combobox显示值为customerTable指定字段
        ComBoxCause2.ValueMember = "CAUSE2_ID"  ‘设置comboBox值为customerTable指定字段

 

‘下面这个函数用来获取SQL连接字符串 

Function getSqlConstr()  '获取SQL连接字符串函数
        Dim sqlconstr As String = "Data Source=" & SQLserverIP & ";Initial Catalog=" & dataBase & ";User Id=sa;Password=ncunicom;"
        Return sqlconstr
    End Function

 

你可能感兴趣的:(VB.NET编程)