最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
解决在执行数据库查询中的特殊字符的问题
时间:2022-06-30 10:07:30 编辑:袖梨 来源:一聚教程网
在进行数据库的查询时,会经常遇到这样的情况:
例如想在一个用户数据库中查询他的用户名和他的密码,
但恰好该用户使用的名字和密码中有特殊的字符,例如单引号,“|”号
双引号或者连字符“&”
例如他的名字是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) & "'"
例如想在一个用户数据库中查询他的用户名和他的密码,
但恰好该用户使用的名字和密码中有特殊的字符,例如单引号,“|”号
双引号或者连字符“&”
例如他的名字是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) & "'"
相关文章
- 人们熟悉的寄居蟹属于以下哪种分类 神奇海洋11月21日答案 11-21
- 第五人格11.22共研服有什么更新 11月22日共研服更新内容介绍 11-21
- 原神恰斯卡怎么培养 11-21
- 无期迷途四星装束是谁 11-21
- 王者荣耀帝丹高中校服怎么获得 11-21
- 光遇姆明季后续版本怎么玩 11-21