乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      SqlParameter的作用與用法

       昵稱10504424 2013-02-19
      1. 一般來說,在更新DataTable或是DataSet時(shí),如果不采用SqlParameter,那么當(dāng)輸入的Sql語句出現(xiàn)歧義時(shí),如字符串中含有單引號(hào),程序就會(huì)發(fā)生錯(cuò)誤,并且他人可以輕易地通過拼接Sql語句來進(jìn)行注入攻擊。  
      2.    
      3.   
      4.   
      5. string sql = "update Table1 set name = 'Pudding' where ID = '1'";//未采用SqlParameter   
      6.   
      7. SqlConnection conn = new SqlConnection();  
      8.   
      9. conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";//連接字符串與數(shù)據(jù)庫有關(guān)   
      10.   
      11. SqlCommand cmd = new SqlCommand(sql, conn);  
      12.   
      13. try  
      14.   
      15. {  
      16.   
      17.     conn.Open();  
      18.   
      19.     return(cmd.ExecuteNonQuery());  
      20.   
      21. }  
      22.   
      23. catch (Exception)  
      24.   
      25. {  
      26.   
      27.     return -1;  
      28.   
      29.     throw;  
      30.   
      31. }  
      32.   
      33. finally  
      34.   
      35. {  
      36.   
      37.     conn.Close();  
      38.   
      39. }   
      40.   
      41.   
      42. 上述代碼未采用SqlParameter,除了存在安全性問題,該方法還無法解決二進(jìn)制流的更新,如圖片文件。通過使用SqlParameter可以解決上述問題,常見的使用方法有兩種,Add方法和AddRange方法。  
      43.    
      44. 一、Add方法  
      45.    
      46.   
      47.   
      48. SqlParameter sp = new SqlParameter("@name""Pudding");  
      49.   
      50. cmd.Parameters.Add(sp);  
      51.   
      52. sp = new SqlParameter("@ID""1");  
      53.   
      54. cmd.Parameters.Add(sp);   
      55.   
      56.   
      57.   該方法每次只能添加一個(gè)SqlParameter。上述代碼的功能是將ID值等于1的字段name更新為Pudding(人名)。  
      58.    
      59. 二、AddRange方法  
      60.    
      61.   
      62.   
      63. SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@name""Pudding"), new SqlParameter("@ID""1") };  
      64.   
      65. cmd.Parameters.AddRange(paras);   
      66.   
      67.   
      68.   顯然,Add方法在添加多個(gè)SqlParameter時(shí)不方便,此時(shí),可以采用AddRange方法。  
      69.    
      70.   下面是通過SqlParameter向數(shù)據(jù)庫存儲(chǔ)及讀取圖片的代碼。  
      71.    
      72.   
      73.   
      74. public int SavePhoto(string photourl)  
      75.   
      76. {  
      77.   
      78.     FileStream fs = new FileStream(photourl, FileMode.Open, FileAccess.Read);//創(chuàng)建FileStream對(duì)象,用于向BinaryReader寫入字節(jié)數(shù)據(jù)流   
      79.   
      80.     BinaryReader br = new BinaryReader(fs);//創(chuàng)建BinaryReader對(duì)象,用于寫入下面的byte數(shù)組   
      81.   
      82.     byte[] photo = br.ReadBytes((int)fs.Length); //新建byte數(shù)組,寫入br中的數(shù)據(jù)   
      83.   
      84.     br.Close();//記得要關(guān)閉br   
      85.   
      86.     fs.Close();//還有fs   
      87.   
      88.     string sql = "update Table1 set photo = @photo where ID = '0'";  
      89.   
      90.     SqlConnection conn = new SqlConnection();  
      91.   
      92.     conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";  
      93.   
      94.     SqlCommand cmd = new SqlCommand(sql, conn);  
      95.   
      96.     SqlParameter sp = new SqlParameter("@photo", photo);  
      97.   
      98.     cmd.Parameters.Add(sp);  
      99.   
      100.     try  
      101.   
      102.     {  
      103.   
      104.         conn.Open();  
      105.   
      106.         return (cmd.ExecuteNonQuery());  
      107.   
      108.     }  
      109.   
      110.     catch (Exception)  
      111.   
      112.     {  
      113.   
      114.         return -1;  
      115.   
      116.         throw;  
      117.   
      118.     }  
      119.   
      120.     finally  
      121.   
      122.     {  
      123.   
      124.         conn.Close();  
      125.   
      126.     }  
      127.   
      128. }  
      129.   
      130.    
      131.   
      132. public void ReadPhoto(string url)  
      133.   
      134.     {  
      135.   
      136.         string sql = "select photo from Table1 where ID = '0'";  
      137.   
      138.         SqlConnection conn = new SqlConnection();  
      139.   
      140.         conn.ConnectionString = "Data Source=.\\SQLExpress;Integrated Security=true;AttachDbFilename=|DataDirectory|\\Database.mdf;User Instance=true";  
      141.   
      142.         SqlCommand cmd = new SqlCommand(sql, conn);  
      143.   
      144.         try  
      145.   
      146.         {  
      147.   
      148.             conn.Open();  
      149.   
      150.             SqlDataReader reader = cmd.ExecuteReader();//采用SqlDataReader的方法來讀取數(shù)據(jù)   
      151.   
      152.             if (reader.Read())  
      153.   
      154.             {  
      155.   
      156.                 byte[] photo = reader[0] as byte[];//將第0列的數(shù)據(jù)寫入byte數(shù)組   
      157.   
      158.                 FileStream fs = new FileStream(url,FileMode.CreateNew);創(chuàng)建FileStream對(duì)象,用于寫入字節(jié)數(shù)據(jù)流  
      159.   
      160.                 fs.Write(photo,0,photo.Length);//將byte數(shù)組中的數(shù)據(jù)寫入fs   
      161.   
      162.                 fs.Close();//關(guān)閉fs   
      163.   
      164.             }  
      165.   
      166.             reader.Close();//關(guān)閉reader   
      167.   
      168.         }  
      169.   
      170.         catch (Exception ex)  
      171.   
      172.         {  
      173.   
      174.             throw;  
      175.   
      176.         }  
      177.   
      178.         finally  
      179.   
      180.         {  
      181.   
      182.             conn.Close();  
      183.   
      184.         }  
      185.   
      186.     }  
      187.   
      188. }  

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多