在下SQL Script,大家都知道使用Parameter來傳入參數會比較安全,但在各種不同的DB使用下,Parameter的符號也會不同.


 


使用ADO.Net for Microsoft SQL


符號 = @


EX :


Select * from testtable where id=@id


 


使用ODO.Net for Oracle


符號 = :


EX :


Select * from testtable where id=:id


補充 :


OracleMS SQL有點不同,如果傳入的值是空字串,Oracle要把它轉為DBNull.Value,MS SQL不用.


cmd.Parameter.Add(“id”,DBNull.Value)


不然會跳出這樣的Exception


Parameter 'id': No size set for variable length data type: String


 


使用ODBC / OLEDB


使用符號 = ?


EX :


Select * from testable where id=?


補充 :


這個可以發現到一點,參數名稱只有符號?,後面沒有參數名稱,那怎麼去對應到Parameter?,它是對應到Parameter.Add的先後加入順序,所以跟加入的順序有關.所以在Add,順序與Script裡的順序要相同.


 


以上的Parameter如果加入的數量與Sql Script裡使用的Parameter數量不同時,是會觸發Exception.


EX :


少傳參數時 :


部份變數未被連結


多傳參數時 :


變數名稱?號碼無效


 


參考 :


SqlCommand.Parameters屬性


OracleCommand.Parameters屬性


Null in Oracle


OdbcCommand.Parameters屬性


OleDbCommand.Parameters屬性

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 jeffyeh 的頭像
    jeffyeh

    jeffyeh

    jeffyeh 發表在 痞客邦 留言(0) 人氣()