呵呵,这是一个典型的数据读取器的问题.可以提供给你一种2个combobox的方法
先选职位,有此职位的人则显示在combobox2中
具体的代码如下
imports system.data.sqlclient
Public Class Form1
Inherits System.Windows.Forms.Form
'windows窗体自动生成代码
dim conn as new sqlconnection("server=.;database=Student;integrated security=sspi")
dim comm as new sqlcommand
dim ds as new dataset
dim da as new sqldataadapter
dim reader1 as sqldatareader
dim i as integer
'load事件中的代码
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = "select * from 职工信息"
reader1 = comm.ExecuteReader()
If reader1.HasRows Then
If reader1.Read Then
For i = 0 To reader1.FieldCount - 1
ComboBoxsel.Items.Add(reader1.GetName(i))
Next
End If
reader1.Close()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
End If
'combobox1中selectindexchanged的代码
ComboBoxfill.Items.Clear()
ComboBoxfill.Text = ""
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
comm.Connection = conn
comm.CommandType = CommandType.Text
comm.CommandText = "select distinct " & ComboBoxsel.SelectedItem & " from 职工信息 "
reader1 = comm.ExecuteReader()
If reader1.HasRows Then
While reader1.Read
ComboBoxfill.Items.Add(reader1(0)) '这个地方与表的字段有关系,要修改
End While
End If
reader1.Close()
If conn.State = ConnectionState.Open Then
conn.Close()
End If
注意:要是多个表的话,需注意各个表之间的联系