佇列(Queue) ,堆疊(Stack)

2014年10月30日 星期四

佇列(Queue)是用先進先出的方式處理物件的集合,例如到銀行排隊,先排的人先處理;而堆疊(Stack )是後進先出的集合,例如玩撲克牌排遊戲時,發牌時是從整疊的最上一張拿取。

                佇列(Queue)常用的方法如下
                名稱 說明
                Count 取得佇列中目前的項目數量
                Dequeue 從佇列前端取出一個項目,同時將其移除
                Enqueue 從佇列尾端加入一個項目
                Peek 從佇列前端取出一個項目,但不移除          
           
                堆疊(Stack)常用的方法如下
                名稱 說明
                Count 取得堆疊中目前的項目數量
                Pop 從堆疊最頂端取出一個項目,同時將其移除
                Push 從堆疊最頂端加入一個項目
                Peek 從堆疊最頂端取出一個項目,但不移除

Queue用法

 
Queue myQueue = new Queue();
myQueue.Enqueue("第一項");
myQueue.Enqueue("第二項");
myQueue.Enqueue("第三項");
myQueue.Enqueue("第四項");

Console.WriteLine("Queue");
while (myQueue.Count > 0)
{
    Console.WriteLine(myQueue.Dequeue());
}
Stack用法
 
Stack myStack = new Stack();
myStack.Push("第一項");
myStack.Push("第二項");
myStack.Push("第三項");
myStack.Push("第四項");

while (myStack.Count > 0) 
{
    Console.WriteLine(myStack.Pop());
}
結果如下:

Read more...

Object doesn't support property or method 'toLowerCase' in IE8

2014年10月14日 星期二

在IE8裡,若型態不是string的話,轉小寫時會發生錯誤


解法:

testObj.toString().toLowerCase();

這樣就ok啦

Read more...

Procedure傳入array參數範例 in Oracle

2014年10月6日 星期一

以下為Procedure傳入array參數範例:
Procedure部份

create or replace PACKAGE  PKG_TEST AS
TYPE NUMBER_ARRAY IS TABLE OF number INDEX BY BINARY_INTEGER;
TYPE STRING_ARRAY IS TABLE OF VARCHAR(200) INDEX BY BINARY_INTEGER;

 procedure GET_ORDERLIST
 (
  P_ORDERLISTID in PKG_TEST.STRING_ARRAY,
  P_RETURNCURSOR OUT SYS_REFCURSOR
 );

/

 procedure GET_ORDERLIST
 (
   P_ORDERLISTID in PKG_TEST.STRING_ARRAY,
   P_RETURNCURSOR OUT SYS_REFCURSOR
 ) as
    varSQL varchar(4000) := '';
    var_OrderIds string(4000) := '''0''';
   begin   
  
  for i in P_ORDERLISTID.first .. P_ORDERLISTID.last loop
   var_OrderIds := var_OrderIds || ',''' ||  P_ORDERLISTID(i) || '''';
  end loop;

  varSQL := 'select * from Order where orderid  in (' || var_OrderIds || ')';

  DBMS_OUTPUT.PUT_LINE(varSQL);
  OPEN P_RETURNCURSOR FOR varSQL;

 end GET_ORDERLIST;

end PKG_TEST

/


C#程式部份

public List GetOrderList(List theSearchList)
{
 var oracleCommand = new OracleCommand();
 oracleCommand.Connection = (OracleConnection)command.Connection;
 oracleCommand.CommandType = CommandType.StoredProcedure;
 oracleCommand.CommandText = "PKG_TEST.GET_ORDERLIST";

 var arryIds = new OracleParameter
 {
  ParameterName = "@P_ORDERLISTID",
  OracleDbType = OracleDbType.Varchar2,
  CollectionType = OracleCollectionType.PLSQLAssociativeArray,
  Value = theSearchList.ToArray(),
  Size = theSearchList.Count(),
  Direction = ParameterDirection.Input
 };
 oracleCommand.Parameters.Add(arryIds);
 oracleCommand.Parameters.Add("@P_RETURNCURSOR", OracleDbType.RefCursor, ParameterDirection.Output);
 oracleCommand.ExecuteNonQuery();

 var dataReader = ((OracleRefCursor)oracleCommand.Parameters["@P_RETURNCURSOR"].Value).GetDataReader();

 var ListDto = new List();

 while (dataReader.Read())
 {
  var dto = new MollyBetOrderBetDto();
  if (!dataReader["orderid"].Equals(DBNull.Value)) dto.OrderID = dataReader["orderid"].ToString();
  if (!dataReader["price"].Equals(DBNull.Value)) dto.Price = Convert.ToDecimal(dataReader["price"]);
  ListDto.Add(dto);
 }

 #region CloseAndDisposeReaderAndCommand
 if (!dataReader.IsNull())
 {
  dataReader.Close();
  dataReader.Dispose();
 }
 oracleCommand.Connection.Close();
 oracleCommand.Connection.Dispose();
 if (!oracleCommand.IsNull())
  oracleCommand.Dispose();
 #endregion
 
 return ListDto;
}

遇到如果需要傳入空array可參考
https://community.oracle.com/message/4126678#4126678
https://community.oracle.com/thread/1000596
http://docs.oracle.com/html/E15167_01/OracleParameterClass.htm#i1012269

Read more...

遇到的Oracle錯誤訊息筆記

2014年10月2日 星期四


錯誤訊息為:
--------------------------------------------------------------------------------------------------------------------------
ORA-00911:invalid character (字元無效)
--------------------------------------------------------------------------------------------------------------------------
00911. 00000 -  "invalid character"
*Cause:    identifiers may not start with any ASCII character other than
           letters and numbers.  $#_ are also allowed after the first
           character.  Identifiers enclosed by doublequotes may contain
           any character other than a doublequote.  Alternative quotes
           (q'#...#') cannot use spaces, tabs, or carriage returns as
           delimiters.  For all other contexts, consult the SQL Language
           Reference Manual.

查到的原因為我在串SQL字串裡,有加逗號,只要把逗號去掉即合;
例:
錯誤
varSQL := 'select * from log ;';
正確
varSQL :='select * from log';


--------------------------------------------------------------------------------------------------------------------------
PLS-00306: wrong number or types of arguments
--------------------------------------------------------------------------------------------------------------------------
最近在Call Procedure,有發生以下的問題,請注意程式給的Parameters參數是否跟Procedure裡面定義的一樣,有時候不小心沒注意到就會發生錯誤,這要注意一下。

錯誤訊息:
 System.Exception: ORA-06550: line 1, column 7: PLS-00306: wrong number or types of arguments in call to 'ICS_USER_PAUSE' ORA-06550: line 1, column 7: PL/SQL: Statement ignored

例: SQL
 procedure GET_ORDERLIST
 (
  P_ORDERLISTID in PKG_TEST.STRING_ARRAY,
  P_RETURNCURSOR OUT SYS_REFCURSOR
 );
程式
var oracleCommand = new OracleCommand();
 oracleCommand.Connection = (OracleConnection)command.Connection;
 oracleCommand.CommandType = CommandType.StoredProcedure;
 oracleCommand.CommandText = "PKG_TEST.GET_ORDERLIST";

 var arryIds = new OracleParameter
 {
  ParameterName = "@P_ORDERLISTID",
  OracleDbType = OracleDbType.Varchar2,
  CollectionType = OracleCollectionType.PLSQLAssociativeArray,
  Value = theSearchList.ToArray(),
  Size = theSearchList.Count(),
  Direction = ParameterDirection.Input
 };
 //Parameters一定要跟Procedure一樣才不會出錯喔
 oracleCommand.Parameters.Add(arryIds);   
 oracleCommand.Parameters.Add("@P_RETURNCURSOR", OracleDbType.RefCursor, ParameterDirection.Output);
 oracleCommand.ExecuteNonQuery();
--------------------------------------------------------------------------------------------------------------------------
ORA-06550:第1行,第7個欄位
--------------------------------------------------------------------------------------------------------------------------
PL/SQL: Statement ignored
原來是我把回傳的int型態給為回傳cursor型態,所以才會造成這樣的錯誤。
解決方法:
將cursor回傳型態改為int32型態即可。
另外也有可能是Store Procdeure沒有權限,所以也要檢查一下喔。

Read more...

foreach裡有條件之改寫

2014年10月1日 星期三

利用lambda改寫可讓原來的程式更為精簡,請看以下的範例 原來寫法

var result = CacheService.Current.GetAllMarketType();
var entities = new List();
if (result != null)
{
 // Make up model data from service return.
 foreach (MarketType marketType in result)
 {
  if (marketType.Status != MarketTypeStatus.DELETED)
  {
   entities.Add(
    new MarketTypeEntity(marketType)
    {
     Id = marketType.id,
     From = marketType.From,
     To = marketType.To,
     Interval = marketType.Interval,
     MaximumRange = marketType.maximumRange,
     MinimumRange = marketType.minimumRange,
     Status = marketType.Status
    });
  }
 }
}

用lambda可改為

var result = CacheService.Current.GetAllMarketType();
var entities = new List();
if (result != null)
{
  entities.AddRange(
  result.Where(o => o.Status != MarketTypeStatus.DELETED)
  .Select(marketType => new MarketTypeEntity(marketType)
  {
   Id = marketType.MarketTypeId,
   From = marketType.From,
   To = marketType.To,
   Interval = marketType.Interval,
   MaximumRange = marketType.maximumRange,
   MinimumRange = marketType.minimumRange,
   Status = marketType.Status
  })
 );
}

Read more...