C# 从组合框中删除重复项从SQL Server自动增值

C# 从组合框中删除重复项从SQL Server自动增值,c#,sql,sql-server,combobox,C#,Sql,Sql Server,Combobox,我正在使用SQL Server 2012并使用VS2013在C#中开发一个Windows窗体应用程序。我有自动将项目从SQL Server添加到combobox的代码: void comboboxadd() { SqlConnection cnn = new SqlConnection(global::BeautyShop.Properties.Settings.Default.BeautyMaConnectionString); cnn.Open(); SqlComm

我正在使用SQL Server 2012并使用VS2013在C#中开发一个Windows窗体应用程序。我有自动将项目从SQL Server添加到combobox的代码:

void comboboxadd()
{
    SqlConnection cnn = new SqlConnection(global::BeautyShop.Properties.Settings.Default.BeautyMaConnectionString);
    cnn.Open();

    SqlCommand cmm = new SqlCommand("select SupName from MPham group by SupName order by SupName asc", cnn);

    try
    {
        SqlDataReader dr = cmm.ExecuteReader();

        while (dr.Read())
        {
            comboBox2.Items.Add(dr["SupName"]);
        }

        dr.Close();
        dr.Dispose();
    }
    catch (Exception ex)
    {
    }
}
我的问题是:当我在
SupName
列(不是主键)上有相同的项时,该项也将添加到组合框中

例如:

SupName   
-----------
ABC  
Joli  
ABC  
Str  
Joli  
组合框项目列表为:

-----------    
ABC  
Joli  
ABC  
Str  
Joli  
-----------  
我希望我的组合框列表如下所示:

ABC  
Joli  
Str  
我该怎么做?

感谢您的支持。

通过链接您的SQL查询来选择不同的项,仅如下所示

SqlCommand cmm = new SqlCommand(
   @"select Distinct SupName 
       from MPham 
   group by SupName 
   order by SupName asc", cnn);
你也可以试试这个

    void comboboxadd()
    {
        SqlConnection cnn = new SqlConnection(global::BeautyShop.Properties.Settings.Default.BeautyMaConnectionString);
        cnn.Open();
        SqlCommand cmm = new SqlCommand("select SupName from MPham group by SupName order by SupName asc", cnn);
        try
        {
            SqlDataReader dr = cmm.ExecuteReader();
            while (dr.Read())
            {
                //Check here if item does not exist then add it.
                if(!comboBox2.Items.Contains(dr["SupName"]))
                     comboBox2.Items.Add(dr["SupName"]);
            }
            dr.Close();
            dr.Dispose();
        }
        catch (Exception ex)
        {

        }
    }
您只需更改查询:

无需使用
分组依据

试一试


请参见此处的About

这是一个解决方案。非常感谢。
select distinct SupName from MPham order by SupName asc