hashtable的问题。期待着能有人帮忙解决!先谢谢了!
我想把从数据库里取出来的数据加到hashtable里面,然后去实现循环替换,但是
这样写while(dr1.Read())
{
hs.Add(dr1[ "ID "].ToString(),dr1[ "Dir_name "].ToStrig());
hs.Add(dr1[ "ID "].ToString(),dr1[ "Province_name "].ToStrig());
编译的时候出错,请问要怎样来处理这个问题?
//////////////////////////////
以下是程序的部分代码:
string ConStr = "Server=(local);DataBase=ad_class;Uid=sa;Pwd= ";
string prostr= "select * from Province_manager ";
SqlConnection Con = new SqlConnection(ConStr);
Con.Open();
SqlCommand Cmd = new SqlCommand(prostr,Con);
SqlDataReader dr1 = Cmd.ExecuteReader();
Hashtable hs = new Hashtable();
if(dr1.HasRows)
{
while(dr1.Read())
{
hs.Add(dr1[ "ID "].ToString(),dr1[ "Dir_name "].ToStrig());
hs.Add(dr1[ "ID "].ToString(),dr1[ "Province_name "].ToStrig());
}
}
------解决方案--------------------一個鍵存兩個值?
用NameValueCollection吧?
------解决方案--------------------public class Address{
private string province_name;
private string dir_name;
public string Province_name{
get{...}
set{...}
}
public string Dir_name{
get{...}
set{...}
}
}
实现:
if(dr1.HasRows)
{
while(dr1.Read())
{
Address addr = new Address();
addr.Dir_name = dr1[ "Dir_name "].ToStrig();
addr.Province_name = dr1[ "Province_name "].ToStrig();
hs.Add(dr1[ "ID "].ToString(),addr);
}
}
手写的,可能有误,但方法就是这样的,当然用struct也行
------解决方案--------------------while(dr1.Read())
{
hs.Add(dr1[ "ID "].ToString()+ "_Dir ",dr1[ "Dir_name "].ToStrig());
hs.Add(dr1[ "ID "].ToString()+ "_Pro ",dr1[ "Province_name "].ToStrig());
}
取得时候也按照ID_Dir或ID_Pro的方式来取
------解决方案--------------------我觉得你的思路可能本身不对
试试下面的:
while(dr1.Read())
{
string [] s=new string[]{dr1[ "Dir_name "].ToStrig(),dr1[ "Province_name "].ToStrig()};
hs.Add(dr1[ "ID "].ToString(),s);
}
这样按照id把数据都保存下来了
------解决方案-------------------- foyuan(暴走零零漆) ,你那方法怎么取出数据来呢?期盼你的回答。。。
====================
stringp[] strArr = MyHashtable[MyHashKey] as string[];
string Dir_name = strArr[0];
// ..