怎样去掉列中重复的项?

Selina_kk 2012-05-11 01:17:27
实践作业:要求用VB2010+ACCESS2007做一个选择省出现对应城市的二级联动

现已完成的代码如下:
Public Class Form1

Dim connectionstring As String ''定义连接字符串


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

''对连接数据库的字符串赋值
connectionstring = "Provider = Microsoft.ACE.OLEDB.12.0;Data Source=" & My.Application.Info.DirectoryPath & "\MyDB.accdb;" '路径为E:\VBtext\text\text\bin\MyDB.accdb
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''控件连接数据库并显示数据
Dim querystring As String = "select * from cs"
Dim conn As New OleDb.OleDbConnection(connectionstring)
Try
conn.Open()
Dim cmd As New OleDb.OleDbCommand(querystring, conn)
Dim reader As OleDb.OleDbDataReader
reader = cmd.ExecuteReader() '执行读取命令

ComboBox1.Items.Clear() '清空现有内容
While (reader.Read())
ComboBox1.Items.Add(reader.GetValue(1)) '添加省份
End While

reader.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub

End Class


运行后的结果如图:



现在想要做的是:把省这个下拉框中的重复项去掉

我知道在SQL语句中省前加DISTINCT即可,可我的程序该怎么弄呀?

各位大虾帮帮忙哦~!~~~~~不胜感激!~~~~~~~~~先谢过啦!~~~~~~~
...全文
143 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
newxdlysk 2012-05-11
  • 打赏
  • 举报
回复
While (reader.Read())
if(ComboBox1.Items.Contains(reader.GetValue(1)))
continue
ComboBox1.Items.Add(reader.GetValue(1)) '添加省份
End While
bdmh 2012-05-11
  • 打赏
  • 举报
回复
Add之前,先用IndexOf检查是否存在

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧