詳解C#中SqlParameter的作用與用法
一般來說,在更新DataTable或是DataSet時(shí),如果不采用SqlParameter,那么當(dāng)輸入的Sql語句出現(xiàn)歧義時(shí),如字符串中含有單引號(hào),程序就會(huì)發(fā)生錯(cuò)誤,并且他人可以輕易地通過拼接Sql語句來進(jìn)行注入攻擊。
string sql
= "update
Table1 set name = 'Pudding' where ID = '1'";//未采用SqlParameter
SqlConnection
conn = new SqlConnection();
conn.ConnectionString
= "Data
Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";//連接字符串與數(shù)據(jù)庫有關(guān)
SqlCommand
cmd = new SqlCommand(sql,
conn);
try
{
conn.Open();
return(cmd.ExecuteNonQuery());
}
catch (Exception)
{
return -1;
throw;
}
finally
{
conn.Close();
}
上述代碼未采用SqlParameter,除了存在安全性問題,該方法還無法解決二進(jìn)制流的更新,如圖片文件。通過使用SqlParameter可以解決上述問題,常見的使用方法有兩種,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方法。
下面是通過SqlParameter向數(shù)據(jù)庫存儲(chǔ)及讀取圖片的代碼。
public int SavePhoto(string photourl)
{
FileStream
fs = new FileStream(photourl,
FileMode.Open, FileAccess.Read);//創(chuàng)建FileStream對象,用于向BinaryReader寫入字節(jié)數(shù)據(jù)流
BinaryReader
br = new BinaryReader(fs);//創(chuàng)建BinaryReader對象,用于寫入下面的byte數(shù)組
byte[]
photo = br.ReadBytes((int)fs.Length);//新建byte數(shù)組,寫入br中的數(shù)據(jù)
br.Close();//記得要關(guān)閉br
fs.Close();//還有fs
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();//采用SqlDataReader的方法來讀取數(shù)據(jù)
if (reader.Read())
{
byte[]
photo = reader[0] as byte[];//將第0列的數(shù)據(jù)寫入byte數(shù)組
FileStream
fs = new FileStream(url,FileMode.CreateNew);創(chuàng)建FileStream對象,用于寫入字節(jié)數(shù)據(jù)流
fs.Write(photo,0,photo.Length);//將byte數(shù)組中的數(shù)據(jù)寫入fs
fs.Close();//關(guān)閉fs
}
reader.Close();//關(guān)閉reader
}
catch (Exception
ex)
{
throw;
}
finally
{
conn.Close();
} }}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:DevExpress TreeList 常見問題解決方法
欄 目:C#教程
下一篇:C#與PHP的md5計(jì)算結(jié)果不同的解決方法
本文標(biāo)題:詳解C#中SqlParameter的作用與用法
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/6052.html
您可能感興趣的文章
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并打開的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁頁面的方法
- 01-10C#實(shí)現(xiàn)由四周向中心縮小的窗體退出特效
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#中DataGridView常用操作實(shí)例小結(jié)
- 01-10C#編程獲取資源文件中圖片的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10C#利用反射技術(shù)實(shí)現(xiàn)去掉按鈕選中時(shí)的邊框效果
- 01-10C#中查找Dictionary中的重復(fù)值的方法
- 01-10C#中TreeView實(shí)現(xiàn)適合兩級(jí)節(jié)點(diǎn)的選中節(jié)點(diǎn)方法


閱讀排行
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 08-05織夢dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法


