把对Accessl 数据操作写成了类 可以直接添加到项目中引用,快速建立或者小项目而言比较方便 举例(非查询语句): 添加货物 public int IntoDGoods(Entity.EGoods model) { SqlParameter[] para = new SqlParameter[]{ new SqlParameter("@GoodsID",model.GoodsID), new SqlParameter("@GoodsName",model.GoodsName), }; int
附登陆代码: using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace 图书馆管理信息系统 { /// /// Form1 的摘要说明。 /// pub lic class login : Syst
新建一个表:
create table abc
(
id int IDENTITY(1,1) NOT NULL,
name nvarchar(100) ,
sex nvarchar(10)
)
insert into abc values(‘asf','男')
insert into abc values(‘ai','女')
创建表格完成。
新建一个存储过程:
create procedure selbyid
(
id int,
thename nvarchar(100) output
)
开发的时候为了方便快速,经常会使用SQL语句拼接的方式,这往往让不法分子有了可乘之机,利用漏洞进行SQL注入,做一些不可描述的事情
SqlCommand cmd = new SqlCommand();
cmd.CommandText = select * from user where username=' + username + ' and password=' + password + ';
所以我们必须丢弃上面这种方式,使用SqlParameter进行参数化,这样才能提升代码健壮性
S
SqlParameter带参数的增删改查语句,可以防止注入.有时候写sql语句的时候会根据方法传进来的参数来判断sql语句中where条件的参数. 一般方法 DAL层方法 代码如下: public UserInfo GetAll(UserInfo a) { string strSql = “select id,name,code,password from [tb].[dbo].[User] where 1=1”; strSql += ” and [id]=id”; strSql += ” a
按常规的思路,我们会这样写 代码如下:String searchName =”Sam”; String strSql = “select * FROM Table1 where Name like ‘%Name%’ “; SqlParameter[] parameters = { new SqlParameter(“Name”, searchName) }; 但结果是查询不到结果,跟踪代码也没有发现错误,又不想用字符串拼接的方式(防止攻击)。于是跟踪了Sql的执行,发现问题在于Sql给参数自动
一般来说,在更新DataTable或是DataSet时,如果不采用SqlParameter,那么当输入的Sql语句出现歧义时,如字符串中含有单引号,程序就会发生错误,并且他人可以轻易地通过拼接Sql语句来进行注入攻击。
string sql
= update
Table1 set name = 'Pudding' where ID = '1';//未采用SqlParameter
SqlConnection
conn = new SqlConnection();
conn.Connect