sql语句主要操作于有条件的的增加,删除,修改或查询但在有多种可变的查询条件下,SQL语句的长度就不一定了,那就需要我们的SQL语句拼接了。比如:string sqlstr= "SELECT 条件1,条件2,条件3,条件4 FROM 表1 s1 INNER JOIN 表2 where";在程序中进行多表查询的时候由于SQL语句的长度是可变的,我们就需要字符串拼接了。

sqlstr += " (S1.S_ID like @query or S1.S_NAME like @query or S1.S_Mobile like @query or S1.S_Address like @query or S1.S_Crad like @query)      and";

因为不知道要拼接多少SQL语句,所以在每条SQL语句后边需加AND 。

SqlParameter name = new SqlParameter("@query", "%" + textBox2.Text + "%");
  comm.Parameters.Add(name);

然后再拼接下一条SQL语句。

在最后拼接的SQL语句后边写上:

comm.CommandText = sqlstr.TrimEnd(new char[] { 'a', 'n', 'd', ' ' });

因为TrimEnd是从当前System.string对象移除数组中指定的一组字符的所有尾部匹配项,

所以,我们需要再上边定义一个数组 new char[]{'a','n','d'' '};

如果不移除AND,程序会报错。

转载于:https://www.cnblogs.com/yjh-bl/p/3980047.html

SQL server中的SQL语句拼接相关推荐

  1. 【转】在SQL Server中通过SQL语句实现分页查询

    在SQL Server中通过SQL语句实现分页查询 2008年01月06日 星期日 12:28 建立表: CREATE TABLE [TestTable] ( [ID] [int] IDENTITY ...

  2. 在SQL Server中使用SQL Coalesce函数

    This article explores the string manipulation using SQL Coalesce function in SQL Server. 本文探讨了在SQL S ...

  3. SQL Server中的SQL语句优化与效率问题

    很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解.比如: select * from table1 where name='zhan ...

  4. 在SQL Server中插入IN-T-SQL语句

    In this article, we will go deeply through the INSERT INTO statement by showing the different syntax ...

  5. 如何从SQL Server中的SELECT语句更新

    In this article, we will learn different methods that are used to update the data in a table with th ...

  6. 如何在SQL Server中创建SQL依赖关系图

    Deleting or changing objects may affect other database objects like views or procedures that depends ...

  7. 转储sql文件_在Linux上SQL Server中更改SQL转储文件位置

    转储sql文件 In this article, we will talk about SQL Dump files and the process to change the dump direct ...

  8. SQL server中使用SQL语句创建表基础步骤

    基本代码 创建 StudentInfo 表: create table StudentInfo (-- 设置主键,并添加标识sID int not null primary key identity( ...

  9. SQL Server中查看SQL句子执行所用的时间

      set   statistics   profile   on      set   statistics   io   on      set   statistics   time   on ...

  10. SQL server中的SELECT查询语句执行顺序

    各位大牛们好第一次写博客有点小激动!以后我会把自己的心得分享给大家,求各种评论 SQL server 中SELSECT查询语句的执行顺序如下: (8) SELECT   (9) DISTINCT (1 ...

最新文章

  1. Cache 工作原理、Cache 一致性,你想知道的都在这里
  2. SprintBoot中如何构造Bean原理分析
  3. java 蓝桥杯算法训练 秘密行动
  4. 小度智能音箱维修点_小度智能音箱——联通智慧生活语音服务入口
  5. Vim新手必看:Vim 命令图解
  6. HTTP Status 404 -(tomcat,springmvc,ModelAndView)
  7. OpenSSL生成RSA公私钥(java)
  8. SharePoint And Ajax Technology(2):Ajax Control Toolkit学习
  9. 问题 1020: [编程入门]猴子吃桃的问题
  10. [矩阵论] 上三角阵的逆(如果有)则也是上三角阵
  11. android加音乐歌词代码,Android简易音乐播放器实现代码
  12. arm-linux驱动开发学习7
  13. c语言中大写a对应的数字,123456789a
  14. iPhone手机连接蓝牙鼠标和蓝牙键盘
  15. Python取某个目录下的所有的EXCEL
  16. windows7以上平台 NDISFilter 网卡过滤驱动开发
  17. PowerBulider-Working with validation rules
  18. error C4996: ‘strcpy‘: This function or variable may be unsafe. Consider using strcpy_s instead.
  19. 7牛-qshel的一些使用
  20. 如何检查Mac配备的显卡(GPU)?

热门文章

  1. ajax无法访问,Ajax不能跨域访问的解决方案
  2. python实现的个人税后工资计算器
  3. Java8 Lambda之Collectors.toMap
  4. 修改服务器后账套不存在,金蝶KIS专业版环境配置常见问题
  5. c语言中按字节运算,C语言中位运算的巧用
  6. plsql快捷执行方式_UG编程必备的快捷键,收藏转发!
  7. 红外倒车雷达原理图_黑科技 | 5分钟看懂无人驾驶汽车和激光雷达
  8. mysql 字段内容大小写_mysql查询字段内容无法区分大小写问题
  9. c++ strcmp函数
  10. spring-data学习上