SqlParameter
生活随笔
收集整理的這篇文章主要介紹了
SqlParameter
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
使用add方法增加cmd的parameter,有更實用的地方——當網站需要操作數據庫的時候,在數據操作類里面寫上相應的參數,用于處理諸如gridview里帶出來的參數,會比較好。
回頭貼代碼。以及更多的學習。
?
?
一般來說,在更新DataTable或是DataSet時,如果不采用SqlParameter,那么當輸入的Sql語句出現歧義時,如字符串中含有單引號,程序就會發生錯誤,并且他人可以輕易地通過拼接Sql語句來進行注入攻擊。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | 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";//連接字符串與數據庫有關 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方法
| 1 2 3 4 | 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方法
| 1 2 | SqlParameter[] paras = new?SqlParameter[] { new?SqlParameter("@name", "Pudding"), new?SqlParameter("@ID", "1") }; cmd.Parameters.AddRange(paras); |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | public?int?SavePhoto(string?photourl) { ??? FileStream fs = new?FileStream(photourl, FileMode.Open, FileAccess.Read);//創建FileStream對象,用于向BinaryReader寫入字節數據流 ??? BinaryReader br = new?BinaryReader(fs);//創建BinaryReader對象,用于寫入下面的byte數組 ??? byte[] photo = br.ReadBytes((int)fs.Length); //新建byte數組,寫入br中的數據 ??? br.Close();//記得要關閉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的方法來讀取數據 ??????????? if?(reader.Read()) ??????????? { ??????????????? byte[] photo = reader[0] as?byte[];//將第0列的數據寫入byte數組 ??????????????? FileStream fs = new?FileStream(url,FileMode.CreateNew);創建FileStream對象,用于寫入字節數據流 ??????????????? fs.Write(photo,0,photo.Length);//將byte數組中的數據寫入fs ??????????????? fs.Close();//關閉fs ??????????? } ??????????? reader.Close();//關閉reader ??????? } ??????? catch?(Exception ex) ??????? { ??????????? throw; ??????? } ??????? finally ??????? { ??????????? conn.Close(); ??????? } ??? } } |
轉載于:https://www.cnblogs.com/wingler/p/3556844.html
總結
以上是生活随笔為你收集整理的SqlParameter的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 念 2013-8-6 (写于爸爸走之后
- 下一篇: js中时间的获取