表结构如下
ID  UPID name
1   0    a
2   0    b
5   1    c
7   1    d
9   2    e
10  2    f
12  5    g
检查UPID为0的时候,是否有两条记录,如果已经有两条了,则检查UPID为1的记录是否有两条,有的话在检查UPID为2的时候,然后在检查5,这样的递归,该怎么写? 

解决方案 »

  1.   

    DataTable dt = 这个数据表;
    bool exists = false;//是否已存在
    int SameCount = 0;
    string Item = "";
    for(int i = 0; i < dt.Rows.Count; i++)
    {
     if(dt.Rows[i]["UPID"].ToString() == Item)
     {
       if(SameCount > 2)
       {
         //已经有两项!
         //相应操作...
         SameCount = 0;
         continue;//继续查找不同的项
       }
       SameCount++;//找到第一项
     }
     Item = dt.Rows[i]["UPID"].ToString();
     if(SameCount > 2) SameCount = 0;
    }
      

  2.   

    DataSet ds = new DataSet();
    // 把表数据 Fill 到了 ds
    ...void Test()
    {
    ArrayList tempAL = new ArrayList();
    for( int i=0;i<ds.Talbes[0].Rows.Count;i++)
    {
    if( !tempAL.Contains(ds.Talbes[0].Rows[i]["upid"].ToString()))
    {
    tempAL.Add(ds.Talbes[0].Rows[i]["upid"].ToString());
    }

    } for( int i=0;i<tempAL.Count;i++)
    {
    DataRow[] foundRow = ds.Tables[0].Select("upid='" + tempAL[i].ToString() + "'");
    Console.Write("UPID 为 " + tempAL[i].ToString() + " 的记录:" + foundRow.Length.ToString() + "条!");
    }}
      

  3.   

    select min(upid) from 表
    group by upid
    having count(*)<2
      

  4.   

    用下面的语句查出所有upid有两条的upid.
    select upid from t_table group by upid having count(*) = 2
    然后你可进行下一步处理。