热门标签 | HotTags
当前位置:  开发笔记 > 数据库 > 正文

SqlServer特殊字符转换&查询

Codesnip,currentlyinusedinmyproject:summaryEncodetheuser-input(withspecialcharacter)intoSQLquerystatementspecialcharacterlike:,[,,_,%...etcsummaryparamnamestrValueuser-inputparampar

Codesnip, currently in used in my project: /// summary /// Encode theuser-input (with special character) into SQL query statement /// special character like : ',[,/,_,%...etc /// /summary /// param name="strValue" user-input /param /// par

Codesnip, currently in used in my project:

///

/// Encode theuser-input (with special character) into SQL query statement

/// special character like : ',[,/,_,%...etc

///

/// user-input

///if it is encode for like statement

///SQL query statement
publicstaticstring sqlEncode(string strValue, bool isLikeStatement)

{

string rtStr = strValue;

if (isLikeStatement)

{

rtStr = strValue.Replace("[", "[[]"); //此句一定要在最前

rtStr = rtStr.Replace("_", "[_]");

rtStr = rtStr.Replace("%", "[%]");

rtStr = rtStr.Replace(@"/", "////");

}

rtStr = rtStr.Replace("'", "''");

return rtStr;

}

===============================ppll的分割线==================================

查询SqlServer特殊字符 原文:Here

我们都知道SQL查询过程中,单引号“'”是特殊字符,所以在查询的时候要转换成双单引号“''”。

但这只是特殊字符的一个,在实际项目中,发现对于like操作还有以下特殊字符:下划线“_”,百分号“%”,方括号“[]”以及尖号“^”。

其用途如下:

下划线:用于代替一个任意字符(相当于正则表达式中的 ?

百分号:用于代替任意数目的任意字符(相当于正则表达式中的 *

方括号:用于转义(事实上只有左方括号用于转义,右方括号使用最近优先原则匹配最近的左方括号)

尖号:用于排除一些字符进行匹配(这个与正则表达式中的一样)

以下是一些匹配的举例,需要说明的是,只有like操作才有这些特殊字符,=操作是没有的。

a_b...

a[_]b%

a%b...

a[%]b%

a[b...

a[[]b%

a]b...

a]b%

a[]b...

a[[]]b%

a[^]b...

a[[][^]]b%

a[^^]b...

a[[][^][^]]b%

对于like操作,需要进行以下替换(注意顺序也很重要)

[ -> [[] (这个必须是第一个替换的!!)

% -> [%] (这里%是指希望匹配的字符本身包括的%而不是专门用于匹配的通配符)

_ -> [_]

^ -> [^]

===============================ppll的分割线==================================

引用:Here

SQL encode and decode Function

2007-07-05 14:31

Function SQL_encode(strContent)
If isnull(strContent) = False Then
strCOntent= replace(strContent, """", """)
strCOntent= replace(strContent, "'", "'")
strCOntent= replace(strContent, "+", "+")
strCOntent= replace(strContent, "*", "*")
strCOntent= replace(strContent, "-", "-")
strCOntent= replace(strContent, "=", "=")
strCOntent= replace(strContent, "<", "<")
strCOntent= replace(strContent, ">", ">")
strCOntent= replace(strContent, "%", "&#37;")
strCOntent= replace(strContent, "_", "&#95;")
SQL_encode = strContent
End If
End Function

Function SQL_decode(strContent)
If isnull(strContent) = False Then
strCOntent= replace(strContent, """, """")
strCOntent= replace(strContent, "&#39;", "'")
strCOntent= replace(strContent, "&#43;", "+")
strCOntent= replace(strContent, "&#42;", "*")
strCOntent= replace(strContent, "&#45;", "-")
strCOntent= replace(strContent, "&#61;", "=")
strCOntent= replace(strContent, "<", "<")
strCOntent= replace(strContent, ">", ">")
strCOntent= replace(strContent, "&#37;", "%")
strCOntent= replace(strContent, "&#95;", "_")
SQL_Decode = strContent
End If
End Function

edition 2006

-------------------------------------------------------------------

'transform any SQL operators to their ascii equivalent
function SQL_encode(strContent)

if isnull(strContent) = false then

'transform sql operators to ascii equivalents
strCOntent= replace(strContent, "'", "|Q|")
strCOntent= replace(strContent, """", "|QQ|")
strCOntent= replace(strContent, "+", "|PLUS|")
strCOntent= replace(strContent, "*", "|STAR|")
strCOntent= replace(strContent, "-", "|MINUS|")
strCOntent= replace(strContent, "=", "|EQUALS|")
strCOntent= replace(strContent, "<", "|LEFT|")
strCOntent= replace(strContent, ">", "|RIGHT|")
strCOntent= replace(strContent, "%", "|PERCENT|")
strCOntent= replace(strContent, "_", "|UNDER|")
strCOntent= replace(strContent, "/", "|BACKS|")
strCOntent= replace(strContent, "/", "|FRONTS|")

SQL_encode = strContent

end if

end function

'tranform ascii characters to their SQL equivalent
function SQL_decode(strContent)

if isnull(strContent) = false then

'transform sql operators
strCOntent= replace(strContent, "|Q|", "'")
strCOntent= replace(strContent, "|QQ|", """")
strCOntent= replace(strContent, "|PLUS|", "+")
strCOntent= replace(strContent, "|STAR|", "*")
strCOntent= replace(strContent, "|MINUS|", "-")
strCOntent= replace(strContent, "|EQUALS|", "=")
strCOntent= replace(strContent, "|LEFT|", "<")
strCOntent= replace(strContent, "|RIGHT|", ">")
strCOntent= replace(strContent, "|PERCENT|", "%")
strCOntent= replace(strContent, "|UNDER|", "_")
strCOntent= replace(strContent, "|BACKS|", "/")
strCOntent= replace(strContent, "|FRONTS|", "/")

SQL_Decode = strContent

end if

end function


推荐阅读
  • 本文详细介绍如何使用Python进行配置文件的读写操作,涵盖常见的配置文件格式(如INI、JSON、TOML和YAML),并提供具体的代码示例。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文探讨了适用于Spring Boot应用程序的Web版SQL管理工具,这些工具不仅支持H2数据库,还能够处理MySQL和Oracle等主流数据库的表结构修改。 ... [详细]
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • 在使用 DataGridView 时,如果在当前单元格中输入内容但光标未移开,点击保存按钮后,输入的内容可能无法保存。只有当光标离开单元格后,才能成功保存数据。本文将探讨如何通过调用 DataGridView 的内置方法解决此问题。 ... [详细]
  • 本文详细介绍了如何在 Linux 平台上安装和配置 PostgreSQL 数据库。通过访问官方资源并遵循特定的操作步骤,用户可以在不同发行版(如 Ubuntu 和 Red Hat)上顺利完成 PostgreSQL 的安装。 ... [详细]
  • 如何在PostgreSQL中查看数据表
    本文将指导您使用pgAdmin工具连接到PostgreSQL数据库,并展示如何浏览和查找其中的数据表。通过简单的步骤,您可以轻松访问所需的表结构和数据。 ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
  • 本文介绍了如何通过 Maven 依赖引入 SQLiteJDBC 和 HikariCP 包,从而在 Java 应用中高效地连接和操作 SQLite 数据库。文章提供了详细的代码示例,并解释了每个步骤的实现细节。 ... [详细]
  • 在使用SQL Server进行动态SQL查询时,如果遇到LIKE语句无法正确返回预期结果的情况,通常是因为参数传递方式不当。本文将详细探讨这一问题,并提供解决方案及相关的技术背景。 ... [详细]
  • 本文介绍如何通过创建替代插入触发器,使对视图的插入操作能够正确更新相关的基本表。涉及的表包括:飞机(Aircraft)、员工(Employee)和认证(Certification)。 ... [详细]
  • MySQL缓存机制深度解析
    本文详细探讨了MySQL的缓存机制,包括主从复制、读写分离以及缓存同步策略等内容。通过理解这些概念和技术,读者可以更好地优化数据库性能。 ... [详细]
  • SQLite 动态创建多个表的需求在网络上有不少讨论,但很少有详细的解决方案。本文将介绍如何在 Qt 环境中使用 QString 类轻松实现 SQLite 表的动态创建,并提供详细的步骤和示例代码。 ... [详细]
  • 精选30本C# ASP.NET SQL中文PDF电子书合集
    欢迎订阅我们的技术博客,获取更多关于C#、ASP.NET和SQL的最新资讯和资源。 ... [详细]
author-avatar
HE-KILL-MY-EGO
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有