先是申明一个公共的:public Hashtable Hashtable1=new Hashtable();
SqlDataAdapter myDataAdapter=new SqlDataAdapter("select a,b from table",Cnn);
DataSet myDs=new DataSet();
SqlCommandBuilder myCommandBuilder=new SqlCommandBuilder(myDataAdapter);
myDataAdapter.Fill(myDs,"table");
Hashtable1.Clear();
for(int i=0;i<myDs.Tables["table"].Rows.Count;i++)
{
string strkbh=myDs.Tables["table"].Rows[i][0].ToString().Trim();
string strcgq=myDs.Tables["table"].Rows[i][1].ToString().Trim();
if(Hashtable1.ContainsKey(strkbh))
{
Hashtable1.Remove(strkbh);
string str=Hashtable1[strkbh].ToString()+strcgq;
Hashtable1.Remove(strkbh);
Hashtable1.Add(strkbh,str);
}
else
{
Hashtable1.Add(strkbh,strcgq);
}
}
如果大家看不懂就请贴出你的代码,也可以发问,谢谢,希望大家积极参与,问题解决马上散分!!!

解决方案 »

  1.   

    Hashtable1.Remove(strkbh);
    /*
      Hashtable1[strkbh] is empty ?
    */
    string str=Hashtable1[strkbh].ToString()+strcgq;
    Hashtable1.Remove(strkbh);
    Hashtable1.Add(strkbh,str);
      

  2.   

    TO:youngc527(C.Young) 
    if(Hashtable1.ContainsKey(strkbh))
    {
    Hashtable1.Remove(strkbh);
    string str=Hashtable1[strkbh].ToString()+strcgq;
    Hashtable1.Remove(strkbh);
    Hashtable1.Add(strkbh,str);
    }
    else
    {
    Hashtable1.Add(strkbh,strcgq);
    }
    这是在一个循环里的,如果它先执行的是else语句,然后再执行if语句的话,Hashtable1[strkbh]就不会是空了!!!
      

  3.   

    看我的注释
    for(int i=0;i<myDs.Tables["table"].Rows.Count;i++)
    {
    string strkbh=myDs.Tables["table"].Rows[i][0].ToString().Trim();
    string strcgq=myDs.Tables["table"].Rows[i][1].ToString().Trim();
    if(Hashtable1.ContainsKey(strkbh))
    {
    // 你先remove了,所以跟着就取不到了
    //Hashtable1.Remove(strkbh);
    string str=Hashtable1[strkbh].ToString()+strcgq;
    Hashtable1.Remove(strkbh);
    Hashtable1.Add(strkbh,str);
    }
    else
    {
    Hashtable1.Add(strkbh,strcgq);
    }
    }
      

  4.   

    int count = myDs.Tables["table"].Rows;
    for(int i=0;i<count;i++)
    {
    DataRow row = myDs.Tables["table"].Rows[i];
    string strkbh=row[0].ToString().Trim();
    string strcgq=row[1].ToString().Trim();
    if(Hashtable1.ContainsKey(strkbh))
    {
    Hashtable1[strkbh] = Hashtable1[strkbh].ToString()+strcgq;
    }
    else
    {
    Hashtable1.Add(strkbh,strcgq);
    }
    }
      

  5.   

    就是啊   remove之后就取不到了  
      

  6.   

    LZ 应该说明白是准备干什么》》》if(Hashtable1.ContainsKey(strkbh))
    {
    Hashtable1[strkbh] = Hashtable1[strkbh] + strcgq;
    }
    else
      

  7.   

    我把问题说明白点,我就不写我的代码了,因为我的代码通过测试是有问题的.我要从数据库里提取两个数据,分别是a,b,表名为table.然后保存到Hashtable1里,a为Hashtable1的键,b为Hashtable1的值,数据库提取出来的有可能一个键只有一个值,也有可能一个键有多个值,数据库里的数据就以键值对的方式保存到Hashtable1里,然后再用Hashtable1和Hashtable2进行比较,如果Hashtable1里的键在Hashtable2里也有就比较值,有相同的值就输出"OK".
    这就是我要实现的整个过程,希望大家给予帮助,谢谢!!!
      

  8.   

    Hashtable1.Remove(strkbh);
    //你已把“strkbh”给remove掉了,后面的操作肯定执行不了啦
    string str=Hashtable1[strkbh].ToString()+strcgq;
    Hashtable1.Remove(strkbh);
      

  9.   

    你不是把strkbh这个键值给移除了吗?
      

  10.   

    为什么要用HashTable这么低级封装的类呢?用Dictionary不好吗?
      

  11.   

    楼主hash表的键值是不能重复的,所以,如果有一键多值的情况你是不能存入hashtable的,请多看看《数据结构》吧,另外,你先remove了,就取不到值了,所以测试会失败,一定失败的,其实你要解决的就是找出重复的记录,完全可以用sql语句解决,方法可以到网上搜索,很多的。你的解决问题的方向都错了。肯定进死胡同了。年轻人,还是大学的基础知识重要呀。