Parameter 对象
生活随笔
收集整理的這篇文章主要介紹了
Parameter 对象
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Parameter 對象代表與基于參數(shù)化查詢或存儲過程的 Command 對象相關(guān)聯(lián)的參數(shù)或自變量.在參數(shù)化查詢時。使用Parameter對象還有利于防止注入。
Parameter 對象屬性與方法:
Name 屬性可設置或返回參數(shù)名稱;
Value 屬性可設置或返回參數(shù)值;
Attributes 和 Direction、Precision、NumericScale、Size 以及 Type 屬性可設置或返回參數(shù)特性;
AppendChunk 方法可將長整型二進制或字符數(shù)據(jù)傳遞給參數(shù)//定義SQL查詢語句 ?string queryString = "Select au_id,au_fname,au_lname,state from authors where state=@state"; //創(chuàng)建并設置SqlCommand SqlCommand dbCommand = new SqlCommand(); dbCommand.Connection = dbConnection; dbCommand.CommandType = CommandType.Text; dbCommand.CommandText = queryString; //設置參數(shù)@state SqlParameter dbParameter_state = new SqlParameter(); dbParameter_state.ParameterName = "@state"; dbParameter_state.Value = state; dbParameter_state.DbType = DbType.StringFixedLength; //向SqlCommmand中添加@state dbCommand.Parameters.Add(dbParameter_state); //Add() 方法, 4個參數(shù) dbCommand.Parameters.Add("@state", DbType.String, 50, state); //Add() 方法, 4個參數(shù) dbCommand.Parameters.Add("@state", state); //AddWithValue() 方法, 2個參數(shù) dbCommand.Parameters.AddWithValue("@state", state);
Parameter 對象屬性與方法:
Name 屬性可設置或返回參數(shù)名稱;
Value 屬性可設置或返回參數(shù)值;
Attributes 和 Direction、Precision、NumericScale、Size 以及 Type 屬性可設置或返回參數(shù)特性;
AppendChunk 方法可將長整型二進制或字符數(shù)據(jù)傳遞給參數(shù)
Command對象操作Parameter對象代碼:
Command對象操作Parameter對象簡寫代碼:
Add(), AddWithValue() 兩個方法相比較Add()方法的效率更高些。
原因在于Add()可以有四個參數(shù)。其中有一個參數(shù)是設置參數(shù)的類型,因此程序就不用再判斷參數(shù)的類型。而AddWithValue()方法則要判斷參數(shù)類型
轉(zhuǎn)載于:https://www.cnblogs.com/synge/archive/2009/12/25/1632208.html
總結(jié)
以上是生活随笔為你收集整理的Parameter 对象的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux网络协议栈(四)——链路层(2
- 下一篇: “天昌”的挽歌——逝去的背影(一)