有一个需要用weiservices解决的项目,传输数据一次一条,总共16000,对方用循环调用,每次发送数据编号1、2、3、4、、、、16000我想用一个自定义类型
public class UserTest
{
private string _username = "";
private string _userid = "";
public UserTest(string name, string ids, string sexs, int ages, string birthday)
{
this._username = name;
this._userid = ids;
}
public string userName
{ set { _username = value; }
get { return _username; }
}
public string userId
{
set
{
_userid = value;
}
get
{
return _userid;
}
}
}
然后定义一个静态的HASHTABLE 用来存放 usertest类型
public class ProPertyClass
{
private static Hashtable myht;
public static Hashtable AllStudent
{
set
{
myht = value;
}
get
{
return myht;
}
}
} 用datareader 读取数据 并放入静态的hashtable中 public int getAllUser()
{
SqlDataReader sdr = dba.GetReader("select * from usertest");
Hashtable ht = new Hashtable();
int i = 0;
int j = sdr.FieldCount;
while (sdr.Read())
{
UserTest ut = new UserTest();//在这里一直实例化对象有没有问题?如果有 实现我的想法有什么别的方法吗
ut.userId = sdr[0].ToString();
ut.userName = sdr[1].ToString();
ht.Add(i, ut);//此处我用int 变量作为key 有没有什么问题? I主要是用来取数据 用ht[i]得到一个usertest对象
i++;
}
ProPertyClass.AllStudent = ht;
sdr.Close();
sdr.Dispose();
return j;
}
下面是我的测试 出了点问题
public string getAllUserstirng(string uid)
{
Hashtable ht = ProPertyClass.AllStudent;
UserTest ut = (UserTest)ht[uid];//此处
UserTest uttest = (UserTest)ht["1"];//此处
//上面两处 ut 和 uttest 都为null 但是 两处(UserTest)ht[uid]内显示有数据 并且是正确的
return ut.userName.ToString();
}
我用以上方法在webservices 内传输我自定义的对象,一次16000个存在hashtable 用循环取数据的时候就从静态的hashtable取得,key是int 1\2\3\4\5、、、、16000
遇到问题 ,望高手能解答 我代码中提出的问题,已经这样做的效率?因为我不想打开关闭16000次数据库。
public class UserTest
{
private string _username = "";
private string _userid = "";
public UserTest(string name, string ids, string sexs, int ages, string birthday)
{
this._username = name;
this._userid = ids;
}
public string userName
{ set { _username = value; }
get { return _username; }
}
public string userId
{
set
{
_userid = value;
}
get
{
return _userid;
}
}
}
然后定义一个静态的HASHTABLE 用来存放 usertest类型
public class ProPertyClass
{
private static Hashtable myht;
public static Hashtable AllStudent
{
set
{
myht = value;
}
get
{
return myht;
}
}
} 用datareader 读取数据 并放入静态的hashtable中 public int getAllUser()
{
SqlDataReader sdr = dba.GetReader("select * from usertest");
Hashtable ht = new Hashtable();
int i = 0;
int j = sdr.FieldCount;
while (sdr.Read())
{
UserTest ut = new UserTest();//在这里一直实例化对象有没有问题?如果有 实现我的想法有什么别的方法吗
ut.userId = sdr[0].ToString();
ut.userName = sdr[1].ToString();
ht.Add(i, ut);//此处我用int 变量作为key 有没有什么问题? I主要是用来取数据 用ht[i]得到一个usertest对象
i++;
}
ProPertyClass.AllStudent = ht;
sdr.Close();
sdr.Dispose();
return j;
}
下面是我的测试 出了点问题
public string getAllUserstirng(string uid)
{
Hashtable ht = ProPertyClass.AllStudent;
UserTest ut = (UserTest)ht[uid];//此处
UserTest uttest = (UserTest)ht["1"];//此处
//上面两处 ut 和 uttest 都为null 但是 两处(UserTest)ht[uid]内显示有数据 并且是正确的
return ut.userName.ToString();
}
我用以上方法在webservices 内传输我自定义的对象,一次16000个存在hashtable 用循环取数据的时候就从静态的hashtable取得,key是int 1\2\3\4\5、、、、16000
遇到问题 ,望高手能解答 我代码中提出的问题,已经这样做的效率?因为我不想打开关闭16000次数据库。
1、你既然用了int 变量作为key, 后来用string来当然会出问题。
2、既然是规则编号,用HashTable不是好选择,用string[16001]不是更直接?3、
UserTest ut = ht[uid] as UserTest;
if (ut != null)
{
//todo
}
莫非你怎么1等于"1"?
比如先定义:IList<UserTest> _objs = new List<UserTest>();插入的时候:UserTest ut = new UserTest();
ut.userId = sdr[0].ToString();
ut.userName = sdr[1].ToString();
_objs.Add(UserTest);
这样效率会高很多
反而把 Hashtable 的劣势全部用上了简单地说
Hashtable 用空间来换取查询效率,在 16000 个对象中检索需要的对象,Hashtable 很有优势
从楼主的代码可以看出,楼主使用集合只是用来存贮数据
这种情况下,最好使用泛型集合 IList
如果要向下兼容 .NET Framework 1.1
可以直接使用 UserTest[] 数组