在VB.NET中ComboBox窗体如何获取数据库中的字段?
问题:
例如:我在做学生管理模块时要用ComboBox窗体获取学生的专业字段。我的代码是:
Dim mydataset As DataSet = New DataSet()Dim conn As SqlConnection = New SqlConnection(“Data Source=AIMOON\SQLEXPRESS;Initial Catalog=xscj;Integrated Security=True“)
conn.Open()
Dim comm As SqlCommand = New SqlCommand(“select * from student“, conn)
Dim dataap As SqlDataAdapter = New SqlDataAdapter(comm)
dataap.Fill(mydataset)
ComboBox1.DataSource = mydataset.Tables(0)
ComboBox1.DisplayMember = “zhuanye“
但是运行时就是不能获取数据库student表中的zhuanye这个字段!!!
解释:
DataAdapter 的 SelectCommand 没有设定,缺一句。dataap.selectcommand = comm
所以数据集没有填充。
而且你这么用 select 会出很多重复值,最好用
SELECT DISTINCT * FROM student
不知道你的数据库表结构,用.net2005瞎写的
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim mydataset As DataSet = New DataSet()
Dim conn As New System.Data.SqlClient.SqlConnection
Dim cnstr As String = “Data Source=AIMOON\SQLEXPRESS;Initial Catalog=xscj;Integrated Security=True“
conn.ConnectionString = cnstr
Dim comm As New System.Data.SqlClient.SqlCommand()
comm.CommandText = “SELECT DISTINCT * FROM student“
comm.Connection = conn
Dim dataap As New System.Data.SqlClient.SqlDataAdapter
dataap.SelectCommand = comm
dataap.Fill(mydataset)
ComboBox1.DataSource = mydataset.Tables(“student“)
ComboBox1.DisplayMember = “student“
End Sub
注意点
使用DISTINCT如何去掉重复记录,并查询所有字段?
表(kzw): 
id       xianlu_id       point_name       x                 y                       h                   biaod_id 
  1               19                   草桥     314594.654     494252.073                                 
  2               19                   草桥     314594.654     494252.073                   
  3               19               北宫门     315032.232     492997.054                                               
  4               19               北宫门     315032.232     492997.054                   
  5               19               北宫门     315032.232     492997.054                                 1 
  6               18         北三环路口   311013.473     496936.988                                 1 
  7               18         北三环路口   311013.473     496936.988   
  8               18         北三环路口   311013.473     496936.988     40.77 
筛选结果: 
id       xianlu_id       point_name       x                 y                       h                   biaod_id 
  1               19                   草桥     314594.654     494252.073                                 
  3               19               北宫门     315032.232     492997.054                                               
  5               19               北宫门     315032.232     492997.054                                 1 
  6               18         北三环路口   311013.473     496936.988                                 1 
  7               18         北三环路口   311013.473     496936.988   
  8               18         北三环路口   311013.473     496936.988     40.77 
数据表规律: 
id(自动编号),xianlu_id(数字),point_name(文本),x(文本),y(文本),h(文本),biaod_id(文本)。其中xianlu_id,point_name,x,y,h,biaod_id六个字段唯一确定一个数,为联合主键 
实现的效果: 
通过xianlu_id,point_name,x,y,h,biaod_id六个字段共同作用,去除重复项,但是要保证能查询出id字段 
我的方法: 
方法一:利用distinct去除重复项 
SELECT   distinct   point_name,x,y,h,xianlu_id,biaod_id   from   kzw   WHERE   xianlu_id   =   "&xianlu_id& "   ORDER   BY   point_name   ASC 
结果: 
显示记录正确,但是没有办法查询出id字段 
方法二:建立子查询 
select   *   from   kzw   a   where   not   exists(select   1   from   kzw   where   id   >   a.id   and   point_name   =   a.point_name   and   x=   a.x   and   y   =   a.y   and   h   =   a.h   and   xianlu_id   =   a.xianlu_id   and   biaod_id   =   a.biaod_id)   and   xianlu_id   =   "&xianlu_id& "   ORDER   BY   point_name   ASC 
结果: 
能查询出id字段,但显示记录仍然有很多重复,基本没有筛选 
请各位大侠帮小妹指指招,看看是哪个地方出了问题? 
-----------------------------------------------------
答复:
 
                    
                     
                    
                 
                    
                
 
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号