使用vb.net将单引号替换为double

Gco*_*ode 2 vb.net

我真的需要知道这一点,有可能解决vb.net中的所有文本框并将单引号替换为双引号?因为我在ms访问中插入数据时遇到问题..这是我目前的代码:

Public Module Functions
    Public Sub singleqoute()
        Dim ctrl As Control
        Dim txt As TextBox

        For Each ctrl In ManageItems.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = txt.Text.Replace("'", "''")
            End If
        Next
        For Each ctrl In ManageBorrowers.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = Replace(txt.Text, "'", "''")
            End If
        Next
        For Each ctrl In ManageTransactions.Controls
            If TypeOf ctrl Is TextBox Then
                txt = CType(ctrl, TextBox)
                txt.Text = Replace(txt.Text, "'", "''")
            End If
        Next

    End Sub

End Module
Run Code Online (Sandbox Code Playgroud)

Jim*_*ley 7

在这种情况下,您应该能够使用ControlChars.Quote.另外,考虑在字符串上使用.Replace方法而不是在VB.Net上使用replace运算符:

txt.Text = txt.Text.Replace("'", ControlChars.Quote)
Run Code Online (Sandbox Code Playgroud)

话虽这么说,我怀疑您尝试进行替换的原因是为了避免使用字符串连接创建的格式错误的SQL语句.这是一个名为SQL Injection的潜在安全问题.最好使用参数化查询并避免连接问题.