利用DateSet实现多个数组的快速比较(.net)

                                                                                       
新建1个DataSet ,并添加列a,b
b列用来识别来源
把列的Unique属性设为true

当把数组1添到table,b=1
再把数组2添到table,   b=2
由于完整性
数组2添加以存在的项时会报异常
添加完之后
b=2的就是数组2有数组1没有的项

             

1                      2

  1None.gifusing System;
  2None.gifusing System.Collections.Generic;
  3None.gifusing System.Text;
  4None.gifusing System.Data;
  5None.gifusing System.Collections;
  6None.gifnamespace WindowsApplication1
  7ExpandedBlockStart.gifContractedBlock.gifdot.gif{
  8InBlock.gif    class Ccmp
  9ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 10InBlock.gif        
 11InBlock.gif        System.Data.DataSet ds=new DataSet();
 12InBlock.gif        public System.Data.DataTable tb;
 13InBlock.gif        ArrayList A = new ArrayList();
 14InBlock.gif        ArrayList B = new ArrayList();
 15InBlock.gif        public Ccmp()
 16ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 17InBlock.gif            tb=ds.Tables.Add("cmptb");
 18InBlock.gif            tb.Columns.Add("content");
 19InBlock.gif            tb.Columns["content"].Unique = true;
 20InBlock.gif            tb.Columns.Add("class");
 21InBlock.gif            tb.Columns.Add("id");
 22InBlock.gif            tb.Columns["id"].AutoIncrement = true;
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//* content class id  */
 24InBlock.gif            
 25ExpandedSubBlockEnd.gif        }

 26InBlock.gif        void Tb_Fill_ADD()
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 28InBlock.gif            tb.Clear();
 29InBlock.gif
 30InBlock.gif            for (int i = 0; i < A.Count; i++)
 31ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 32InBlock.gif                tb.Rows.Add(A[i],"1");
 33ExpandedSubBlockEnd.gif            }

 34InBlock.gif            for (int i = 0; i < B.Count; i++)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 36InBlock.gif                try
 37ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 38InBlock.gif                    tb.Rows.Add(B[i], "2");
 39ExpandedSubBlockEnd.gif                }

 40InBlock.gif                catch (Exception)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifcontinue; }
 42ExpandedSubBlockEnd.gif            }

 43InBlock.gif        
 44ExpandedSubBlockEnd.gif        }

 45InBlock.gif        void Tb_Fill_DEL()
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 47InBlock.gif            tb.Clear();
 48InBlock.gif
 49InBlock.gif            for (int i = 0; i < B.Count; i++)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 51InBlock.gif                tb.Rows.Add(B[i], "1");
 52ExpandedSubBlockEnd.gif            }

 53InBlock.gif            for (int i = 0; i < A.Count; i++)
 54ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 55InBlock.gif                try
 56ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
 57InBlock.gif                    tb.Rows.Add(A[i], "2");
 58ExpandedSubBlockEnd.gif                }

 59InBlock.gif                catch (Exception)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gifcontinue; }
 61ExpandedSubBlockEnd.gif            }

 62InBlock.gif
 63ExpandedSubBlockEnd.gif        }

 64ExpandedSubBlockStart.gifContractedSubBlock.gif        public ArrayList Cmp_Add()/**//// 返回B相对A添加了的
 65ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 66InBlock.gif            Tb_Fill_ADD();
 67InBlock.gif            ArrayList temp = new ArrayList();
 68InBlock.gif            for (int i = 0; i < tb.Rows.Count; i++)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 70InBlock.gif                if (tb.Rows[i]["class"].ToString()=="2")
 71InBlock.gif               temp.Add( tb.Rows[i]["content"]);
 72ExpandedSubBlockEnd.gif            }

 73InBlock.gif
 74InBlock.gif            return temp;
 75ExpandedSubBlockEnd.gif        }

 76ExpandedSubBlockStart.gifContractedSubBlock.gif        public ArrayList Cmp_Del()/**////返回B相对A删除了的
 77ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 78InBlock.gif            Tb_Fill_DEL();
 79InBlock.gif            ArrayList temp = new ArrayList();
 80InBlock.gif            for (int i = 0; i < tb.Rows.Count; i++)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                if (tb.Rows[i]["class"].ToString() == "2")
 83InBlock.gif                    temp.Add(tb.Rows[i]["content"]);
 84ExpandedSubBlockEnd.gif            }

 85InBlock.gif
 86InBlock.gif            return temp;
 87ExpandedSubBlockEnd.gif        }

 88InBlock.gif
 89InBlock.gif        public void Clear_AB()
 90ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 91InBlock.gif            A.Clear();
 92InBlock.gif            B.Clear();
 93ExpandedSubBlockEnd.gif        }

 94InBlock.gif        public void Add_A(object content)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 96InBlock.gif            A.Add(content);
 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif        public void Add_B(object content)
 99ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
100InBlock.gif            B.Add(content);
101ExpandedSubBlockEnd.gif        }

102InBlock.gif
103ExpandedSubBlockEnd.gif    }

104ExpandedBlockEnd.gif}

105None.gif
106None.gif

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值