ASP.NET DropDownList数据库中的重复值

时间:2016-07-14 14:39:43

标签: c# sql asp.net

我的数据库就像这样。

obtain name
ask again
verify address


public class Program
{
    public static void Main()
    {
        //Console.WriteLine("Hello World");
        Program.GetReturn("started manually 23:56:22 Skipped: obtain name Completed: verify dob Skipped: ask again Skipped: verify address  Completed: verify country Stopped manually 23:58:55","Skipped:");
    }

    public static string GetReturn(string str,string word)
    {
        string mystr="";
        var myarray=str.Split(' ');

        for(int i = 0; i< myarray.Length-1; i++)
        {

            if(myarray[i]==word)
            {
                mystr=myarray[i+1] + " " +myarray[i+2];
                Console.WriteLine(String.Format("{0} {1}", myarray[i+1],myarray[i+2]));
            }
        }       

        return mystr;
    }

Vehicle Make | Vehicle Model ----------------------------- Toyota | AQUA HONDA | FIT Toyota | Prius ,填充像这样。

DropDownList1

但由于我有重复的数据(2丰田&#39; s),它也会在DropDownList中显示它们。

是否可以过滤并且只有1个丰田?

-PS

我需要它添加我的数据库,因为可能有更多的车辆添加。

2 个答案:

答案 0 :(得分:3)

当然,您可以使用DISTINCT

SqlDataAdapter adapter = new SqlDataAdapter("SELECT DISTINCT VehicleMake FROM VehicleDB", con);

您可能还想添加ORDER BY

string sql = @"SELECT DISTINCT VehicleMake 
               FROM VehicleDB
               ORDER BY VehicleMake ASC";
using(var  adapter = new SqlDataAdapter(sql, con))
{
    // ...
}

答案 1 :(得分:0)

首先,您可以将查询更改为&#34; SELECT VehicleMake FROM VehicleDB,其中VehicleMake&lt;&gt; &#39; FIT Toyota&#39;&#34;并且更改查询不是你的选择,那么你可以做这样的事情

var subjects= ds.Tables["VehicleDB"];
adresse.DefaultView.RowFilter = "VehicleMake LIKE '%TEST%'";

DropDownList1.DataSource = subjects;
DropDownList1.DataTextField = "VehicleMake";
DropDownList1.DataValueField = "VehicleMake";
DropDownList1.DataBind();

甚至这不会工作为数据表应用LINQ - for循环