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相关推荐
- SqlParameter的用法
1.一些面例子为例: List<SqlParameter> listp = new List<SqlParameter>(); listp.Add(ne ...
- SqlParameter参数方式操作数据库(存储过程)
访问数据库: View Code using System; using System.Data; using System.Configuration; using System.Linq; usi ...
- mysql idataparameter_小白不坑爹的asp.net SqlParameter和带参数存储过程运用
IDataParameter[]是SqlParameter[]的接口实例,2种方式的作用是一样的 sqlParameter的构造函数 SqlParameter () 初始化 SqlParameter ...
- 总结:实例化SqlParameter时,如果是字符型,一定要指定size属性,还有制定具体的类型...
源地址 在csdn上逛看到了这篇帖子,值得学习,就需要记录下来,转载给大家看! 以前在实例化SqlParameter时,通常都是用下面的语句,没有设置size属性: new SqlParameter( ...
- SqlParameter参数化查询
上篇博客写了关于重构代码用到的SQLHelper类,这个类包括四种函数,根据是否含参和是否有返回值各分两种.在这里写写传参过程用到的SqlParameter. 如果我们使用如下拼接sql字符串的方式进 ...
- C# 中SqlParameter类的使用方法小结
在c#中执行sql语句时传递参数的小经验 1.直接写入法: 例如: int Id =1; string Name="lui"; cmd.CommandText="inse ...
- 问题解决:SqlParameterCollection只接受非空的SqlParameter类型对象,不接受SqlParameter[]对象
做.net个人版机房收费系统时,遇到了这样一个问题: 既然提到了SqlParameter,那就意味着设置参数处出现了问题,但是到底是哪里出了问题呢,之前运行的时候也没有出错呀,怎么现在又出现了这样的错 ...
- [转]另一个SqlParameterCollection 中已包含 SqlParameter[解决方案]
本着代码重用,减少变量的创建,希望参数能够重复使用 DataBase db = new DataBase(); SqlParameter[] prams ={ db.MakeInPar ...
- SqlParameter的作用与用法
因为通过SQL语句的方式,有时候存在脚本注入的危险,所以在大多数情况下不建议用拼接SQL语句字符串方式,希望通过SqlParameter实现来实现对数据的操作,针对SqlParameter的方式我们同 ...
最新文章
- hector slam matlab,如何使用hector slam算法包
- AliOS Things v1.2.0新特性
- MAC EI Capitan上更新系统自带SVN版本号(关闭SIP方能sudo rm)
- Hystrix配置参数查找方式
- C++ static静态成员变量
- oracle dd if=/dev/zero of=/dev,【转】dd if=/dev/zero of=的含义是什么?Linux 下的dd命令使用详解...
- android权威指南十三章,《Android编程权威指南》第30~32以及第34章读书笔记
- Android 功耗优化(3)---Android后台调度与省电
- signature=31dd91e9b698a2c577ac276c1a6ed0c4,openssl的man中文文档 - 1(转)
- matlab求解pde,matlab求解pde问题
- 蓝桥杯省赛2020 成绩统计
- PySerial库的简单用法
- Sublime中文显示乱码
- cntv.cn今日“开锅” USB3.0初露端倪(每日关注2009.12.28)
- poco http使用
- 天翼云linux上传工具,(一)天翼云主机安装seafile7.0.3
- Petya and Inequiations 题解
- 使用echarts生成漂亮的3D地图
- 用Selenium+xpath爬取京东商城
- stm32流水灯c语言程序,stm32流水灯程序源码