sqlparameter 的用法及作用

來源:互聯網
上載者:User
 

一般來說,在更新DataTable或是DataSet時,如果不採用SqlParameter,那麼當輸入的Sql語句出現歧義時,如字串中含有單引號,程式就會發生錯誤,並且他人可以輕易地通過拼接Sql語句來進行注入攻擊。

 
  1. string sql = "update Table1 set name = 'Pudding' where ID = '1'";//未採用SqlParameter   
  2. SqlConnection conn = new SqlConnection();   
  3. conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";//連接字串與資料庫有關   
  4. SqlCommand cmd = new SqlCommand(sql, conn);   
  5. try   
  6. {   
  7. conn.Open();   
  8. return(cmd.ExecuteNonQuery());   
  9. }   
  10. catch (Exception)   
  11. {   
  12. return -1;   
  13. throw;   
  14. }   
  15. finally   
  16. {   
  17. conn.Close();   
  18. }  

上述代碼未採用SqlParameter,除了存在安全性問題,該方法還無法解決二進位流的更新,片檔案。通過使用SqlParameter可以解決上述問題,常見的使用方法有兩種,Add方法和AddRange方法。

一、Add方法

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

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

二、AddRange方法

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

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

下面是通過SqlParameter向資料庫儲存及讀取圖片的代碼。

 
  1. view sourceprint?01 public int SavePhoto(string photourl)   
  2. {   
  3. FileStream fs = new FileStream(photourl, FileMode.Open, FileAccess.Read);//建立FileStream對象,用於向BinaryReader寫入位元組資料流   
  4. BinaryReader br = new BinaryReader(fs);//建立BinaryReader對象,用於寫入下面的byte數組   
  5. byte[] photo = br.ReadBytes((int)fs.Length); //建立byte數組,寫入br中的資料   
  6. br.Close();//記得要關閉br   
  7. fs.Close();//還有fs   
  8. string sql = "update Table1 set photo = @photo where ID = '0'";   
  9. SqlConnection conn = new SqlConnection();   
  10. conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";   
  11. SqlCommand cmd = new SqlCommand(sql, conn);   
  12. SqlParameter sp = new SqlParameter("@photo", photo);   
  13. cmd.Parameters.Add(sp);   
  14. try   
  15. {   
  16. conn.Open();   
  17. return (cmd.ExecuteNonQuery());   
  18. }   
  19. catch (Exception)   
  20. {   
  21. return -1;   
  22. throw;   
  23. }   
  24. finally   
  25. {   
  26. conn.Close();   
  27. }   
  28. }   
  29. public void ReadPhoto(string url)   
  30. {   
  31. string sql = "select photo from Table1 where ID = '0'";   
  32. SqlConnection conn = new SqlConnection();   
  33. conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";   
  34. SqlCommand cmd = new SqlCommand(sql, conn);   
  35. try   
  36. {   
  37. conn.Open();   
  38. SqlDataReader reader = cmd.ExecuteReader();//採用SqlDataReader的方法來讀取資料   
  39. if (reader.Read())   
  40. {   
  41. byte[] photo = reader[0] as byte[];//將第0列的資料寫入byte數組   
  42. FileStream fs = new FileStream(url,FileMode.CreateNew);建立FileStream對象,用於寫入位元組資料流   
  43. fs.Write(photo,0,photo.Length);//將byte數組中的資料寫入fs   
  44. fs.Close();//關閉fs   
  45. }   
  46. reader.Close();//關閉reader   
  47. }   
  48. catch (Exception ex)   
  49. {   
  50. throw;   
  51. }   
  52. finally   
  53. {   
  54. conn.Close();   
  55. }   
  56. }   
  57. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.