生活随笔
收集整理的這篇文章主要介紹了
SqlParameter的作用与用法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
因?yàn)橥ㄟ^(guò)SQL語(yǔ)句的方式,有時(shí)候存在腳本注入的危險(xiǎn),所以在大多數(shù)情況下不建議用拼接SQL語(yǔ)句字符串方式,希望通過(guò)SqlParameter實(shí)現(xiàn)來(lái)實(shí)現(xiàn)對(duì)數(shù)據(jù)的操作,針對(duì)SqlParameter的方式我們同樣可以將其封裝成一個(gè)可以復(fù)用的數(shù)據(jù)訪問(wèn)類,只是比SQL語(yǔ)句的方式多了一個(gè)SqlParameter的參數(shù)。
AD:
2013大數(shù)據(jù)全球技術(shù)峰會(huì)低價(jià)搶票中
一般來(lái)說(shuō),在更新DataTable或是DataSet時(shí),如果不采用SqlParameter,那么當(dāng)輸入的Sql語(yǔ)句出現(xiàn)歧義時(shí),如字符串中含有單引號(hào),程序就會(huì)發(fā)生錯(cuò)誤,并且他人可以輕易地通過(guò)拼接Sql語(yǔ)句來(lái)進(jìn)行注入攻擊。
string?sql?=?"update?Table1?set?name?=?'Pudding'?where?ID?=?'1'";?SqlConnection?conn?=?new?SqlConnection();? ?conn.ConnectionString?=?"Data?Source=.\\SQLExpress;Integrated?Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User?Instance=true";?SqlCommand?cmd?=?new?SqlCommand(sql,?conn);? ?try? ?{? ?conn.Open();? ?return(cmd.ExecuteNonQuery());? ?}? ?catch?(Exception)? ?{? ?return?-1;? ?throw;? ?}? ?finally? ?{? ?conn.Close();? ?}??上述代碼未采用SqlParameter,除了存在安全性問(wèn)題,該方法還無(wú)法解決二進(jìn)制流的更新,如圖片文件。通過(guò)使用SqlParameter可以解決上述問(wèn)題,常見(jiàn)的使用方法有兩種,Add方法和AddRange方法。
一、Add方法
SqlParameter?sp?=?new?SqlParameter("@name",?"Pudding");? ?cmd.Parameters.Add(sp);? ?sp?=?new?SqlParameter("@ID",?"1");? ?cmd.Parameters.Add(sp);??該方法每次只能添加一個(gè)SqlParameter。上述代碼的功能是將ID值等于1的字段name更新為Pudding(人名)。
二、AddRange方法
SqlParameter[]?paras?=?new?SqlParameter[]?{?new?SqlParameter("@name",?"Pudding"),?new?SqlParameter("@ID",?"1")?};? ?cmd.Parameters.AddRange(paras);??顯然,Add方法在添加多個(gè)SqlParameter時(shí)不方便,此時(shí),可以采用AddRange方法。
下面是通過(guò)SqlParameter向數(shù)據(jù)庫(kù)存儲(chǔ)及讀取圖片的代碼。
view?sourceprint?01?public?int?SavePhoto(string?photourl)? ?{? ?FileStream?fs?=?new?FileStream(photourl,?FileMode.Open,?FileAccess.Read);?BinaryReader?br?=?new?BinaryReader(fs);?byte[]?photo?=?br.ReadBytes((int)fs.Length);??br.Close();?fs.Close();?string?sql?=?"update?Table1?set?photo?=?@photo?where?ID?=?'0'";? ?SqlConnection?conn?=?new?SqlConnection();? ?conn.ConnectionString?=?"Data?Source=.\\SQLExpress;Integrated?Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User?Instance=true";? ?SqlCommand?cmd?=?new?SqlCommand(sql,?conn);? ?SqlParameter?sp?=?new?SqlParameter("@photo",?photo);? ?cmd.Parameters.Add(sp);? ?try? ?{? ?conn.Open();? ?return?(cmd.ExecuteNonQuery());? ?}? ?catch?(Exception)? ?{? ?return?-1;? ?throw;? ?}? ?finally? ?{? ?conn.Close();? ?}? ?}? ?public?void?ReadPhoto(string?url)? ?{? ?string?sql?=?"select?photo?from?Table1?where?ID?=?'0'";? ?SqlConnection?conn?=?new?SqlConnection();? ?conn.ConnectionString?=?"Data?Source=.\\SQLExpress;Integrated?Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User?Instance=true";? ?SqlCommand?cmd?=?new?SqlCommand(sql,?conn);? ?try? ?{? ?conn.Open();? ?SqlDataReader?reader?=?cmd.ExecuteReader();?if?(reader.Read())? ?{? ?byte[]?photo?=?reader[0]?as?byte[];?FileStream?fs?=?new?FileStream(url,FileMode.CreateNew);創(chuàng)建FileStream對(duì)象,用于寫入字節(jié)數(shù)據(jù)流? ?fs.Write(photo,0,photo.Length);?fs.Close();?}? ?reader.Close();?}? ?catch?(Exception?ex)? ?{? ?throw;? ?}? ?finally? ?{? ?conn.Close();? ?}? ?}? ?}??
轉(zhuǎn)載于:https://www.cnblogs.com/rinack/archive/2013/05/11/3073426.html
與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的SqlParameter的作用与用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。