熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

c#中SortedList的使用

2022-06-13   來源: ASP編程 

  對IDictionary進行操作是發現用SortedList也可以等同於 ArryList和Hashtable的結合只是通過Key排序key不允許為空和null

  value可以在效率上沒有測試但是保證是低必定在插入時要比較排序

  通過公用方法得到信息(得到行數據)

  public IDictionary ExecuteDictionary( IDbCommand iCmd )
{
IDataReader reader = null;
try
{
//只讀取一行數據第一行
reader = iCmdExecuteReader( CommandBehaviorSingleRow );
}
catch(Exception e)
{
thisClose( iCmd );
return null;
}

  IDictionary dict = null;

  if(readerRead())
{
int fieldCount = readerFieldCount;
dict = new SortedList( fieldCount );

  for(int i = ; i < fieldCount; i++)
{
dict [readerGetName( i )ToLower()] = readerGetValue( i );
}
}

  readerClose();
readerDispose();
return dict;
}

  //返回list
public SortedList selectSingln()
{
DBCommandText = @ SELECT TOP * FROM products;
DBCommandType = CommandTypeText;
return (SortedList)DBExecuteDictionary();
}

  //遍歷list
private void _BeginRun()
{
_SqlServerLogic logic = new _SqlServerLogic();

  SortedList dic = logicselectSingln();
Hashtable hash = new Hashtable();

  //遍歷sortlist
foreach(DictionaryEntry entry in dic)
{
ResponseWrite( entryKey + *** + entryValue + <br> );

  if( !stringIsNullOrEmpty( entryValueToString() ) )
{
hashAdd( entryKey entryValue );
}
}

  IDictionaryEnumerator item = hashGetEnumerator();

  //遍歷Hashtable
while( itemMoveNext() )
{
ResponseWrite( itemKey ++ itemValue +<br/> );
}

  string [] ary = new string [dicCount];
dicKeysCopyTo( ary );

  ResponseWrite( stringJoin( ary ) );

  //for 遍歷list
for(int i = ; i < dicCount; i++)
{
ResponseWrite( dicGetKey(i)++ dicGetByIndex(i) +<br/> );
}
}


From:http://tw.wingwit.com/Article/program/ASP/201311/21751.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.