国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

詳解C#中SqlParameter的作用與用法

2019-10-29 21:16:34
字體:
供稿:網(wǎng)友

一般來說,在更新DataTable或是DataSet時,如果不采用SqlParameter,那么當(dāng)輸入的Sql語句出現(xiàn)歧義時,如字符串中含有單引號,程序就會發(fā)生錯誤,并且他人可以輕易地通過拼接Sql語句來進行注入攻擊。

string sql = "update Table1 set name = 'Pudding' where ID = '1'";//未采用SqlParameterSqlConnection 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,除了存在安全性問題,該方法還無法解決二進制流的更新,如圖片文件。通過使用SqlParameter可以解決上述問題,常見的使用方法有兩種,Add方法和AddRange方法。

一、Add方法

SqlParameter sp = new SqlParameter("@name","Pudding");cmd.Parameters.Add(sp);sp = new SqlParameter("@ID","1");cmd.Parameters.Add(sp);

該方法每次只能添加一個SqlParameter。上述代碼的功能是將ID值等于1的字段name更新為Pudding(人名)。

二、AddRange方法

SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name","Pudding"),new SqlParameter("@ID","1") };cmd.Parameters.AddRange(paras);

顯然,Add方法在添加多個SqlParameter時不方便,此時,可以采用AddRange方法。

下面是通過SqlParameter向數(shù)據(jù)庫存儲及讀取圖片的代碼。

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í)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到c#教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 景洪市| 平阳县| 宁阳县| 赤峰市| 乌海市| 枣庄市| 陆丰市| 子长县| 云龙县| 大港区| 许昌县| 双牌县| 易门县| 措美县| 黑水县| 张家界市| 松原市| 阳谷县| 班玛县| 许昌市| 哈尔滨市| 霍城县| 麟游县| 临猗县| 筠连县| 元氏县| 铜山县| 沈丘县| 兴文县| 来宾市| 雷波县| 泾阳县| 宝鸡市| 商城县| 鄂伦春自治旗| 绥棱县| 乐至县| 那曲县| 大余县| 许昌市| 长沙县|