使用 vb.net 将单引号替换为双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22328456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace single quote to double using vb.net
提问by Gcode
I really need to know this, it is possible to address all the textbox in vb.net and replace single quotes to double quotes? because im having problem on inserting data in ms access.. this is my current code:
我真的需要知道这一点,可以解决 vb.net 中的所有文本框并将单引号替换为双引号吗?因为我在 ms access 中插入数据时遇到问题..这是我当前的代码:
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
回答by Jim Wooley
You should be able to use ControlChars.Quote in this case. Also, consider using the .Replace method on string rather than the replace operator with VB.Net:
在这种情况下,您应该能够使用 ControlChars.Quote。另外,考虑在字符串上使用 .Replace 方法而不是 VB.Net 的替换运算符:
txt.Text = txt.Text.Replace("'", ControlChars.Quote)
That being said, I suspect the reason you are trying to do the replacement is to avoid malformed SQL statements that you are creating by using string concatenation. This is a potential security issue called SQL Injection. It's better to use parameterized queriesand avoid the concatenation issues.
话虽如此,我怀疑您尝试进行替换的原因是为了避免使用字符串连接创建的格式错误的 SQL 语句。这是一个潜在的安全问题,称为SQL 注入。最好使用参数化查询并避免串联问题。
回答by Yusuf
Instead of ', you can write chr(39).
取而代之的是',您可以编写chr(39).
39 is the ASCII value of '
39 是 ASCII 值 '

