[续LovenDreams ]数据库开发的一些小经验!(值得注意的细节)

kuailexq2000 元老 2003-07-08 10:16:58
{我会整理FAQ}
1.
SQL SERVER 中
所有的输入都应注意尽量禁止在Sql Server中的特殊字符,如 单引号,%,*,#

Public Class StringFormat

'字符串格式化类

Function SqlStrFormat(ByVal strSql As String) As String

'Sql语句方式的字符串格式化函数,采用特殊方式替换掉 sql Wildcard Characters

'该函数使用范围有限

If strSql = "" Then

Return ""

End If

strSql = strSql.Replace("'", "''") '替换单引号

strSql = strSql.Replace("[", "[[]")

strSql = strSql.Replace("%", "[%]")

strSql = strSql.Replace("_", "[_]")

strSql = strSql.Replace("*", "
  • "
  • )

    Return strSql

    End Function

    End Class
    -------------------------------------------------------------------------
    2.ORACLE中
    所有的输入都应注意尽量禁止在Sql的特殊字符,如 单引号
    Public Function CToSqlStrValue(ByVal CurValue As String) As String
    Dim strReturnValue As String

    Try
    If CurValue <> "" Then
    strReturnValue = "'" & CurValue.Replace("'", "''") & "'"
    Else
    strReturnValue = "NULL"
    End If
    Catch myException As System.Exception
    S_Err("CToSqlStrValue", Err)
    End Try

    Return strReturnValue
    End Function
    ------------------------------------------------------------------------
    等等 抛砖引玉(请大家列出各种可能出错的特殊符号)
...全文
59 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kuailexq2000 元老 2004-03-04
  • 打赏
  • 举报
回复
SQL SERVER 中
所有的输入都应注意尽量禁止在Sql Server中的特殊字符,如 单引号,%,*,#

Public Class StringFormat

'字符串格式化类

Function SqlStrFormat(ByVal strSql As String) As String

'Sql语句方式的字符串格式化函数,采用特殊方式替换掉 sql Wildcard Characters

'该函数使用范围有限

If strSql = "" Then

Return ""

End If

strSql = strSql.Replace("'", "''") '替换单引号

strSql = strSql.Replace("[", "[[]")

strSql = strSql.Replace("%", "[%]")

strSql = strSql.Replace("_", "[_]")

strSql = strSql.Replace("*", "
  • "
  • )

    Return strSql

    End Function

    End Class
    -------------------------------------------------------------------------
    2.ORACLE中
    所有的输入都应注意尽量禁止在Sql的特殊字符,如 单引号
    Public Function CToSqlStrValue(ByVal CurValue As String) As String
    Dim strReturnValue As String

    Try
    If CurValue <> "" Then
    strReturnValue = "'" & CurValue.Replace("'", "''") & "'"
    Else
    strReturnValue = "NULL"
    End If
    Catch myException As System.Exception
    S_Err("CToSqlStrValue", Err)
    End Try

    Return strReturnValue
    End Function
    ------------------------------------------------------------------------
    sql语句是一个字符串,所有的变量应该先转换为string
    如dim strSQL as string = "Select * from tableUser where suser = ' " & sUserName & "' " sUserName为变量,当生成sql语句时,需要当作变量
kuailexq2000 元老 2004-03-01
  • 打赏
  • 举报
回复
ding
jwt1982 2003-12-05
  • 打赏
  • 举报
回复
ding
kuailexq2000 元老 2003-12-02
  • 打赏
  • 举报
回复
up
kuailexq2000 元老 2003-07-14
  • 打赏
  • 举报
回复
ok!
abiho 2003-07-08
  • 打赏
  • 举报
回复
sql语句是一个字符串,所有的变量应该先转换为string
如dim strSQL as string = "Select * from tableUser where suser = '" & sUserName & "'" sUserName为变量,当生成sql语句时,需要当作变量
kuailexq2000 元老 2003-07-08
  • 打赏
  • 举报
回复
up!

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧