避免在数据集重名 [英] Avoid duplicate names in a dataset

查看:232
本文介绍了避免在数据集重名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从表中获取数据,并绑定到一个标签,并在GridView下拉,但我想从过滤表中重复的名字,并指定相应的日期到DDL怎么可以这样做?或者是有什么其他选择?

 私有数据集的get()
    {
        字符串SQL =选择编号,名称,RunDate从Historytable
        SqlDataAdapter的大=新SqlDataAdapter的(SQL,CON);
        DataSet的DS =新的DataSet();
        da.Fill(DS);
        返回DS;
    }

 保护无效GridView1_RowDataBound1(对象发件人,GridViewRowEventArgs E)
    {        //什么code在这里?
        }


解决方案

如果您想找回您的HistoryTable只有唯一的名称,你应该使用关键字DISTINCT

  SQL字符串=从Historytable选择不重复的名称;

不过,看到您的更新,我认为你应该查询更改为类似@dasblinkenlight的建议答案,如果你想在第一次约会每个名称

  SQL字符串=选择闵(ID),名称,闵(RunDate)从Historytable组按名称;

或者(如果你想为每个名字的最后一天)

  SQL字符串=选择马克斯(ID),名称,马克斯(RunDate)从Historytable组按名称;

I am fetching data from a table and binding to a label and Drop down in a gridview but i want to filter the duplicate names from the table and assign the corresponding dates to the DDL how can do this? or else is there any other alternative?

 private DataSet get()
    {
        string sql = "select Id,Name,RunDate from Historytable";
        SqlDataAdapter da=new SqlDataAdapter(sql,con);
        DataSet ds=new DataSet();
        da.Fill(ds);
        return ds;
    }

 protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
    { 

        //what to code here?


        }

解决方案

If you want to get back only unique names from your HistoryTable you should use the keyword DISTINCT

string sql = "select DISTINCT Name from Historytable";

however, seeing your update I think you should change your query to something like the proposed answer of @dasblinkenlight, if you want the first date for each name

string sql = "select Min(ID), Name, Min(RunDate) from Historytable group by Name";

or (if you want the last date for each name)

string sql = "select Max(ID), Name, Max(RunDate) from Historytable group by Name";

这篇关于避免在数据集重名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆