我从数据库里查到如下记录放在datetable里:
nh       qh
2010 03 1.500000 0103001 玉米
2010 03 1.200000 0103027 玉米
2010 03 1.750000 0103032 磷酸氢钙
2010 04 1.620000 0103001 玉米
2010 04 11.000000 0103038 LP61W
。 05。。
我想从上面的数据集得到:
03
04
05
即:取qh字段不重复的!
后台怎么怎么写

解决方案 »

  1.   

    select nh from datetable group by nh
      

  2.   

    http://topic.csdn.net/t/20060110/15/4508234.html
      

  3.   

    dataview 自己查查用法
      

  4.   

    用linq查询,或者自己写个算法,遍历下datatable,读出不重复的qh字段值
      

  5.   

    datatable 不是有个select方法  你添入查询条件不就行了
      

  6.   

    这里有一个取distinct数据的方法. static   DataTable   SelectDistinct(string   ReturnTableName,   DataTable   SourceTable,   string   ReturnFieldName,   string   AdditionalFilterExpression) 

    DataTable   dt   =   new   DataTable(ReturnTableName); 
    dt.Columns.Add(ReturnFieldName,   SourceTable.Columns[ReturnFieldName].DataType); 
    object   LastValue   =   null; 
    foreach   (DataRow   dr   in   SourceTable.Select( " ",   ReturnFieldName)) 

    if   (LastValue   ==   null   ||   !(ColumnEqual(LastValue,   dr[ReturnFieldName]))) 

    LastValue   =   dr[ReturnFieldName]; 
    dt.Rows.Add(new   object[]   {   LastValue   }); 


    if   (ds   !=   null) 
    ds.Tables.Add(dt); 
    return   dt; 
    } static   bool   ColumnEqual(object   A,   object   B) 

    //   Compares   two   values   to   see   if   they   are   equal.   Also   compares   DBNULL.Value. 
    //   Note:   If   your   DataTable   contains   object   fields,   then   you   must   extend   this 
    //   function   to   handle   them   in   a   meaningful   way   if   you   intend   to   group   on   them. if   (A   ==   DBNull.Value   &&   B   ==   DBNull.Value)   //   both   are   DBNull.Value 
    return   true; 
    if   (A   ==   DBNull.Value   ||   B   ==   DBNull.Value)   //   only   one   is   DBNull.Value 
    return   false; 
    return   (A.Equals(B));   //   value   type   standard   comparison 

      

  7.   

    你先遍历datetable,找出distinct的qh。
    然后datetable.select(qh in (遍历的结果集))
      

  8.   

    http://support.microsoft.com/default.aspx?scid=kb;en-us;326176
      

  9.   

    不能在SQL里直接写么?
    distinct
      

  10.   

    datatable.DefaultView.ToTable(true,"qh");//设qh为03,04,05字段头
      

  11.   


    这返回只包适这一列不重复的表
    如果要返回多列,且不重重的,可以用datatable.DefaultView.ToTable(true,"qh,XX1,XX2")// 列以逗号分开