C#参数化SQL查询
//写一个存储过程
ALTER PROCEDURE dbo.Infosearch
(
@bmid smallint = null,
@xm varchar()=null,
@xb varchar()=null,
@strage smallint=null,
@endage smallint=null,
@zzmm varchar()=null,
@xl varchar()=null,
@zw varchar()=null
)
AS
/* SET NOCOUNT ON */
declare @sql varchar()
if @bmid is not null
begin
set @sql=' where 部门ID='+Convert(varchar(),@bmid)
end
if @xm is not null
begin
if @sql is not null
set @sql=@sql+' and 姓名like'+@xm
else set @sql=' where 姓名like'+@xm
end
if @xb is not null
begin
if @sql is not null
set @sql=@sql+' and 性别='+@xb
else set @sql=' where 性别='+@xb
end
if @strage is not null
begin
if @sql is not null
set @sql=@sql+' and 年龄between '+Convert(varchar(),@strage)
else set @sql=' where 年龄between '+Convert(varchar(),@strage)
end
if @endage is not null
begin
set @sql=@sql+' and '+Convert(varchar(),@endage)
end
if @zzmm is not null
begin
if @sql is not null
set @sql=@sql+' and 政治面貌='+@zzmm
else set @sql=' where 政治面貌='+@zzmm
end
if @xl is not null
begin
if @sql is not null
set @sql=@sql+' and 学历='+@xl
else set @sql=' where 学历='+@xl
end
if @zw is not null
begin
if @sql is not null
set @sql=@sql+' and 职位='+@zw
else set @sql=' where 职位='+@zw
end
exec('select 职工号,姓名,性别,年龄,学历,婚姻状况,政治面貌from yuangong'+@sql)
RETURN
ALTER PROCEDURE dbo.Infosearch
(
@bmid smallint = null,
@xm varchar()=null,
@xb varchar()=null,
@strage smallint=null,
@endage smallint=null,
@zzmm varchar()=null,
@xl varchar()=null,
@zw varchar()=null
)
AS
/* SET NOCOUNT ON */
declare @sql varchar()
if @bmid is not null
begin
set @sql=' where 部门ID='+Convert(varchar(),@bmid)
end
if @xm is not null
begin
if @sql is not null
set @sql=@sql+' and 姓名like'+@xm
else set @sql=' where 姓名like'+@xm
end
if @xb is not null
begin
if @sql is not null
set @sql=@sql+' and 性别='+@xb
else set @sql=' where 性别='+@xb
end
if @strage is not null
begin
if @sql is not null
set @sql=@sql+' and 年龄between '+Convert(varchar(),@strage)
else set @sql=' where 年龄between '+Convert(varchar(),@strage)
end
if @endage is not null
begin
set @sql=@sql+' and '+Convert(varchar(),@endage)
end
if @zzmm is not null
begin
if @sql is not null
set @sql=@sql+' and 政治面貌='+@zzmm
else set @sql=' where 政治面貌='+@zzmm
end
if @xl is not null
begin
if @sql is not null
set @sql=@sql+' and 学历='+@xl
else set @sql=' where 学历='+@xl
end
if @zw is not null
begin
if @sql is not null
set @sql=@sql+' and 职位='+@zw
else set @sql=' where 职位='+@zw
end
exec('select 职工号,姓名,性别,年龄,学历,婚姻状况,政治面貌from yuangong'+@sql)
RETURN
//判断参数是否为空来决定怎样拼接查询语句
如果是多条件查询的话
存储过程里面就一个参数就够了
这个参数是不定条件查询语句
多条件之中判断那个是否为空 如果为空填充1=1 不为空就为条件
public static IDataReader ExecuteReader(DbCommand comm, string sql, params object[] value)
{
comm.CommandText = sql;
if (value != null && value.Length >= )
{
if (comm.CommandText.IndexOf("?") == -)
{
string[] temp = sql.Split('@');
for (int i = ; i < value.Length; i++)
{
string pName;
if (temp[i + ].IndexOf(" ") > -)
{
pName = "@" + temp[i + ].Substring(, temp[i + ].IndexOf(" "));
}
else
{
pName = "@" + temp[i + ];
}
//pName = "@p" + (i + 1).ToString();
DbParameter p = comm.CreateParameter();
p.DbType = DbType.String;
p.ParameterName = pName;
p.Value = value[i];
comm.Parameters.Add(p);
}
}
else
{
string[] temp = sql.Split('?');
for (int i = ; i < value.Length; i++)
{
temp[i] = temp[i] + "@p" + (i + ).ToString();
string pName = "@p" + (i + ).ToString();
DbParameter p = comm.CreateParameter();
p.DbType = DbType.String;
p.ParameterName = pName;
p.Value = value[i];
comm.Parameters.Add(p);
}
StringBuilder sb = new StringBuilder();
for (int i = ; i < temp.Length; i++)
{
sb.Append(temp[i]);
}
comm.CommandText = sb.ToString();
}
}
if (comm.Connection.State != ConnectionState.Open)
{
comm.Connection.Open();
}
return comm.ExecuteReader(CommandBehavior.CloseConnection);
} 调用的时候类似:ExecuteReaderParams(comm, "select * from xx where id=? and name=?",id,name);
C#参数化SQL查询的更多相关文章
- SQL Server-聚焦深入理解动态SQL查询(三十二)
前言 之前有园友一直关注着我快点出SQL Server性能优化系列,博主我也对性能优化系列也有点小期待,本来打算利用周末写死锁以及避免死锁系列的接着进入SQL Server优化系列,但是在工作中长时间 ...
- SQL Server参数化SQL语句中的like和in查询的语法(C#)
sql语句进行 like和in 参数化,按照正常的方式是无法实现的 我们一般的思维是: Like参数化查询:string sqlstmt = "select * from users whe ...
- SQL Server SQL性能优化之--数据库在“简单”参数化模式下,自动参数化SQL带来的问题
数据库参数化的模式 数据库的参数化有两种方式,简单(simple)和强制(forced),默认的参数化默认是“简单”,简单模式下,如果每次发过来的SQL,除非完全一样,否则就重编译它(特殊情况会自动参 ...
- SQL Server中参数化SQL写法遇到parameter sniff ,导致不合理执行计划重用的一种解决方案
parameter sniff问题是重用其他参数生成的执行计划,导致当前参数采用该执行计划非最优化的现象.想必熟悉数据的同学都应该知道,产生parameter sniff最典型的问题就是使用了参数化的 ...
- 执行原始的 SQL 查询
The Entity Framework Code First API includes methods that enable you to pass SQL commands directly t ...
- 在ADO.NET中使用参数化SQL语句访问不同数据库时的差异
在ADO.NET中经常需要跟各种数据库打交道,在不实用存储过程的情况下,使用参数化SQL语句一定程度上可以防止SQL注入,同时对一些较难赋值的字段(如在SQL Server中Image字段,在Orac ...
- 参数化SQL小认识
在做机房收费系统项目,编写数据连接并访问数据库时,见别人都用了带“@”字符的SQL语句,就很好奇为什么都用这个语句呢?直接拼写SQL语句不是更加方便吗?带着这个问题上网查资料,才知道原来他们用的是参数 ...
- SQL Server-聚焦sp_executesql执行动态SQL查询性能真的比exec好?
前言 之前我们已经讨论过动态SQL查询呢?这里为何再来探讨一番呢?因为其中还是存在一定问题,如标题所言,很多面试题也好或者有些博客也好都在说在执行动态SQL查询时sp_executesql的性能比ex ...
- 不得不看,只有专家才知道的17个SQL查询提速秘诀!
不得不看,只有专家才知道的17个SQL查询提速秘诀! 原创 2018-01-23 布加迪编译 51CTO技术栈 “ 除非你遵循本文介绍的这些技巧,否则很容易编写出减慢查询速度或锁死数据库的数据库代码. ...
随机推荐
- SPI的工作模式口诀[转]
口诀:03升,12降,低取小,高取大 ! 解释: 1. 03升,12降: 0.3模式为上升沿采样,1.2模式为下降沿采样. 2. 低取小,高取大:SCK初始电平为低则模式取小值,SCK初始电平为高则模 ...
- NopCommerce使用Autofac实现依赖注入
NopCommerce的依赖注入是用的AutoFac组件,这个组件在nuget可以获取,而IOC反转控制常见的实现手段之一就是DI依赖注入,而依赖注入的方式通常有:接口注入.Setter注入和构造函数 ...
- POJ1651:Multiplication Puzzle(区间DP)
Description The multiplication puzzle is played with a row of cards, each containing a single positi ...
- jQuery的如何捕捉回车键,改变事件标签
我希望有一个jQuery的解决方案,我必须接近,有什么需要做的? $('html').bind('keypress', function(e) { if(e.keyCode == 13) { retu ...
- 关于InputStream 和String对象之间的相互转换
代码如下: package com.xin.stream; import java.io.BufferedReader; import java.io.ByteArrayInputStream; im ...
- Linux Centos 7 使用yum安装 mysql5.7 (实验成功)
第一部分:安装Mysql5.7 1.下载YUM库 shell > wget http://dev.mysql.com/get/mysql57-community-release-el7-7.no ...
- Unhandled Error in Silverlight Application “Syncfusion.Silverlight.Olap.Gauge.OlapGauge”的类型初始值设定项引发异常
Silverlight 在运行时,如果出现如下错误: 检查生成的xap文件,解压出来,看是否里面包含该DLL:Syncfusion.Silverlight.Olap.Gauge.OlapGauge
- log4net日志文件名输出格式化
日志文件输出格式: 20130626.txt20130627.txt20130628.txt20130629.txt <appender name="rolling" typ ...
- ReactNative学习-滑动查看图片第三方组件react-native-swiper
滑动查看图片第三方组件:react-native-swiper,现在的版本为:1.4.3,该版本还不支持Android. 下面介绍的是该组件的一些用法,可能总结的不完整,希望大家一起来共同完善. 官方 ...
- poj 3689 树形dp
思路: 每个点有三种状态,本身有塔,被子节点的塔覆盖,被父节点的塔覆盖. #include<map> #include<set> #include<cmath> # ...