博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 向SQL里写入非Text类型
阅读量:4502 次
发布时间:2019-06-08

本文共 2980 字,大约阅读时间需要 9 分钟。

 一般来说,在更新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);
  显然,Add方法在添加多个SqlParameter时不方便,此时,可以采用AddRange方法。
  下面是通过SqlParameter向数据库存储及读取图片的代码。
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/liangxiaofeng/p/5829453.html

你可能感兴趣的文章
asp.net 得到一个文件夹下的所有文件夹及子文件夹名,得到所有文件名,文件大小,文件夹大小...
查看>>
从keystore(jks)文件中提取私钥
查看>>
调整数组顺序使奇数位于偶数前面
查看>>
HackerRank "Training the army" - Max Flow
查看>>
jquery next()方法
查看>>
深入剖析js命名空间函数namespace
查看>>
SQLHelper
查看>>
Cocos2d-x 3.0 编译出错 解决 error: expected ';' at end of member declaration
查看>>
Ubuntu12.04下载Repo
查看>>
python基础教程_学习笔记10:异常
查看>>
MATLAB——scatter的简单应用
查看>>
linux下复制粘贴快捷键
查看>>
什么是对象
查看>>
记录开发小程序
查看>>
WinSock服务程序
查看>>
巴西柔术第五课:过腿
查看>>
文件的操作
查看>>
网上图书商城项目学习笔记-007登录功能实现
查看>>
关于mysql的级联删除(之前好多人咨询过我)
查看>>
Linux环境下的C/C+基础调试技术2——程序控制
查看>>