SqlParameter的用法

  1. 一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语句来进行注入攻击。  
  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";//连接字符串与数据库有关  
  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,除了存在安全性问题,该方法还无法解决二进制流的更新,如图片文件。通过使用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.   该方法每次只能添加一个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方法在添加多个SqlParameter时不方便,此时,可以采用AddRange方法。  
  69.    
  70.   下面是通过SqlParameter向数据库存储及读取图片的代码。  
  71.    
  72.   
  73.   
  74. public int SavePhoto(string photourl)  
  75.   
  76. {  
  77.   
  78.     FileStream fs = new FileStream(photourl, FileMode.Open, FileAccess.Read);//创建FileStream对象,用于向BinaryReader写入字节数据流  
  79.   
  80.     BinaryReader br = new BinaryReader(fs);//创建BinaryReader对象,用于写入下面的byte数组  
  81.   
  82.     byte[] photo = br.ReadBytes((int)fs.Length); //新建byte数组,写入br中的数据  
  83.   
  84.     br.Close();//记得要关闭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的方法来读取数据  
  151.   
  152.             if (reader.Read())  
  153.   
  154.             {  
  155.   
  156.                 byte[] photo = reader[0] as byte[];//将第0列的数据写入byte数组  
  157.   
  158.                 FileStream fs = new FileStream(url,FileMode.CreateNew);创建FileStream对象,用于写入字节数据流  
  159.   
  160.                 fs.Write(photo,0,photo.Length);//将byte数组中的数据写入fs  
  161.   
  162.                 fs.Close();//关闭fs  
  163.   
  164.             }  
  165.   
  166.             reader.Close();//关闭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. }  
一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语句来进行注入攻击。
 


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方法
 


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向数据库存储及读取图片的代码。
 


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();

        }

    }

}

 技术交流群:29609188 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值