易截截图软件、单文件、免安装、纯绿色、仅160KB

参数化SQL语句SqlParameter


避免SQL注入的方法有两种:一是所有的SQL语句都存放在存储过程中,这样不但可以避免SQL注入,还能提高一些性能,并且存储过程可以由专门的数据库管理员(DBA)编写和集中管理(这种做法我在一些公司见过),不过这种做法有时候针对相同的几个表有不同条件的查询,SQL语句可能不同,这样就会编写大量的存储过程,所以有人提出了第二种方案:参数化SQL语句。例如我们在本篇中创建的表UserInfo中查找所有女性用户,那么通常情况下我们的SQL语句可能是这样:
select * from UserInfo where sex=0
在参数化SQL语句中我们将数值以参数化的形式提供,对于上面的查询,我们用参数化SQL语句表示为:
select * from UserInfo where sex=@sex
我们再对代码中对这个SQL语句中的参数进行赋值,假如我们要查找UserInfo表中所有年龄大于30岁的男性用户,这个参数化SQL语句可以这么写:
select * from UserInfo where sex=@sex and age>@age
下面是执行这个查询并且将查询结果集以DataTable的方式返回的代码:
C# code
//实例化Connection对象
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=AspNetStudy;Persist Security Info=True;User ID=sa;Password=sa");
//实例化Command对象
SqlCommand command = new SqlCommand("select * from UserInfo where sex=@sex and age>@age", connection);
//第一种添加查询参数的例子
command.Parameters.AddWithValue("@sex", true);
//第二种添加查询参数的例子
SqlParameter parameter = new SqlParameter("@age", SqlDbType.Int);//注意UserInfo表里age字段是int类型的
parameter.Value = 30;
command.Parameters.Add(parameter);//添加参数
//实例化DataAdapter
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable data = new DataTable();


相关文档:

sql 2005 存储过程分页 java 代码

 create PROCEDURE pagelist
@tablename nvarchar(50),
@fieldname nvarchar(50)='*',         
@pagesize int output,--每页显示记录条数
@currentpage int output,--第几页
@orderid nvarchar(50),--主键排序
@sort int,--排序方式,1表示升序,0表示降序排列 ......

SQL数据中运行cmd命令

在sql查询分析器里面是不能直接运行cmd命令的
但是SQL给出了一个接口
--打开高级设置
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
--打开xp_cmdshell扩展存储过程
EXEC sp_configure 'xp_cmdshell', 1
RECONFIGURE
首先 打开一些配置
然后执行你要运行cmd命令
exec master..xp_cmdshell 'net star ......

SQL learning

Five basic search conditions are summarized here:
1) Comparison test
2) Range test
3) Set membership test
4) Pattern matching test (Like)
     The pattern matching test checks to see whether the data value in a column matches a specified pattern
   % mathes any se ......

SQL外键

创建外键约束
CREATE TABLE order_sample
(
orderid int PRIMARY KEY,
cust_id int FOREIGN KEY REFERENCES cuts_sample(cust_id) ON DELETE NO CASCADE
)
ON DELETE--用于控制尝试删除外键相关联的主表指向行时采取的操作
-NO ACTION
删除外键相关联的主表指向行时,报错
-CASCADE
删除外键相关联的主表指向行时 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号