SqlParameter的作用与用法
创始人
2024-07-29 19:10:37
0

一般来说,在更新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. }  

 【编辑推荐】

  1.  ASP.net的身份验证方式FORMS
  2. ASP.Net中保护自定义的服务器控件
  3. 大型高性能ASP.NET系统架构设计
  4. ASP.NET配置文件Web.config详细解释
  5. VB.NET和ASP.NET编码规范

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...