資料庫查詢中的特殊字元的問題

2021-06-22 00:23:20 字數 1772 閱讀 1478

資料庫查詢中的特殊字元的問題

在進行資料庫的查詢時,會經常遇到這樣的情況:

例如想在乙個使用者資料庫中查詢他的使用者名稱和他的密碼,但恰好該使用者使用的名字和密碼中有特殊的字元,例如單引號,「|」號,雙引號或者連字元「&」。

例如他的名字是1"test,密碼是a|&900

這時當你執行以下的查詢語句時,肯定會報錯:

sql = "select * from securitylevel where uid="" & userid & """

sql = sql & " and pwd="" & password & """ 

因為你的sql將會是這樣:

select * from securitylevel where uid="1"test" and pwd="a|&900" 

在sql中,"|"為分割欄位用的,顯然會出錯了。現在提供下面的幾個函式 專門用來處理這些頭疼的東西:

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 if

end function

function sqlfixup(textin)

sqlfixup = replacestr(textin, """, """", 0)

end function

function jetsqlfixup(textin)

dim temp

temp = replacestr(textin, """, """", 0)

jetsqlfixup = replacestr(temp, "|", "" & chr(124) & "", 0)

end function

function findfirstfixup(textin)

dim temp

temp = replacestr(textin, """, "" & chr(39) & "", 0)

findfirstfixup = replacestr(temp, "|", "" & chr(124) & "", 0)

end function 

有了上面幾個函式後,當你在執行乙個sql前,請先使用

sql = "select * from securitylevel where uid="" & sqlfixup(userid) & """

sql = sql & " and pwd="" & sqlfixup(password) & """。

本文完!

資料庫中特殊字元的訪問

特殊字元資料庫的訪問問題 html常用特殊字元 html 原 顯示結果 描述 小於號或顯示標記 大於號或顯示標記 可用於顯示其它特殊字元 引號 已註冊版權 商標半個空白位 乙個空白位 不斷行的空白 html特殊字元編碼大全 往網頁中輸入特殊字元,需在html 中加入以 開頭的字母組合或以 開頭的數字...

資料庫中特殊字元的訪問

1.特殊字元資料庫的訪問問題 html常用特殊字元 html 原 顯示結果 描述 小於號或顯示標記 大於號或顯示標記 可用於顯示其它特殊字元 引號 已註冊版權 商標半個空白位 乙個空白位 不斷行的空白 html特殊字元編碼大全 往網頁中輸入特殊字元,需在html 中加入以 開頭的字母組合或以 開頭的...

資料庫中特殊字元的訪問

特殊字元資料庫的訪問問題 第一種方法就是replace 例如在入庫的時候可以把 換成 把 r n 換成 這樣的話在自動幫定的時候就可以在資料控制項中顯示換行和空格了,但是要注意的是如果是自己寫 進行,要把資料顯示在textbox lable等上面,就要注意了,要把它們替換過來即在用replace把資...