combobox从数据库中读取数据库某一列的值,有好多重复的值,怎样才能去除重复项?
c#代码该怎么写?

解决方案 »

  1.   

    使用select distinct name from table之类的语句
      

  2.   

    第一.要么就是数据库里查时.加一个distinct 过虑重复项.
    第二.如果不在数据库里过滤的话,就用C#写个方法过滤吧! 方法就是一项一项的取出来放到另一个数组中.如果数组中已经存在了,就放弃该项不要放到数组中.最后数组中的就是你要的无重复的数据了.
      

  3.   

    同意2楼和4楼的说法,还是用SQL 语句DISTINCT筛选出来比较快,比较简单。
      

  4.   

    做个判断方法方法:private bool Validate(List<string> li,string str)
    {
        foreach(string s in li)
        {
            if(s == str)
            {
                return false;
            }
        }
        return true;
    }在调用的时候传个集合和读取出来的字符串就OK了:List<string> li = new List<string>();
    SqlDataReader sdr = 获取的SqlDataReader对象;
    while(sdr.Read())
    {
        if(Validate(li,sdr["你的列名"].toString())
        {
            this.combobox1.Items.Add(sdr["你的列名"].toString());
        }
    }
    sdr.Close();
      

  5.   

    while(sdr.Read()) 

        if(Validate(li,sdr["你的列名"].toString()) 
        { 
            this.combobox1.Items.Add(sdr["你的列名"].toString()); 
            li.Add(sdr["你的列名"].toString());
        } 
    } 上面少写一句;
      

  6.   

    sql写的有问题select distinct 
      

  7.   

    select distinct 列名 from 表名
    distinct 去除重复数据