Special characters in database queries

zhaozj2021-02-16  49

##### Copyright reproduced leave thank you ##### Author: Ouyang Dong Jie ##### Email: admin@xzsky.com##### QQ: 7462792 ##### WebSite: www .xzsky.com

This is often encountered when performing a database query: For example, if you want to query his username and his password in a user database, there is a special character in the name and password used by the user, such as single quotes. "|", Double quotes or characters "&". For example, his name is 1 "TEST, the password is A | & 900 When you perform the following query statement, it will definitely report error: SQL =" SELECT * from securityLevel WHERE UID = "" & userid & "" SQL = SQL & "And PWD =" "& Password &" "" Because your SQL will be like this: select * from securitylevel where uid = "1" TEST "and PWD =" a | & 900 "in SQL," | " If the segmentation field is used, it will obviously be wrong. Now provide the following functions to deal with these headaches: Function ReplaceStr (Textin, Byval Searchstr As String, _Byval Replacement AS String, _byval compmode as integer

Dim WorkText As String, Pointer As Integer If IsNull (TextIn) Then ReplaceStr = Null Else WorkText = TextIn Pointer = InStr (1, WorkText, SearchStr, CompMode) Do While Pointer> 0 WorkText = Left (WorkText, Pointer - 1) & Replacement & _ MID (Worktext, Pointer Len (Searchstr) Pointer = INSTR (Pointer Len (Replacement), Worktext, Searchstr, CompMode) loop replaceStr = Worktext End IFEND Function

Function SQLFixUp (Textin) SQLFIXUP = ReplaceStr (Textin, "" "," "" ", 0) DIM TEMP TEMP = ReplaceStr (Textin," "", "" ", 0) Jetsqlfixup = ReplaceStr (Temp, "|" "& chr (124) &", 0) end function

转载请注明原文地址:https://www.9cbs.com/read-23959.html

New Post(0)