SQL 如何在VB代码中编写特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/419554/
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
How can I write special character in VB code
提问by Jon Skeet
I have a Sql statament using special character (ex: ('), (/), (&)) and I don't know how to write them in my VB.NET code. Please help me. Thanks.
我有一个使用特殊字符(例如:(')、(/)、(&))的 Sql 语句,但我不知道如何在我的 VB.NET 代码中编写它们。请帮我。谢谢。
回答by Jon Skeet
Find out the Unicode code point for the character (from http://www.unicode.org) and then use ChrW to convert from the code point to the character. (To put this in another string, use concatenation. I'm somewhat surprised that VB doesn't have an escape sequence, but there we go.)
找出字符的 Unicode 代码点(来自http://www.unicode.org),然后使用 ChrW 将代码点转换为字符。(要将它放在另一个字符串中,请使用连接。我有点惊讶 VB 没有转义序列,但我们去了。)
For example, for the Euro sign (U+20AC) you'd write:
例如,对于欧元符号 (U+20AC),您可以这样写:
Dim euro as Char = ChrW(&H20AC)
The advantage of this over putting the character directly into source code is that your source code stays "just pure ASCII" - which means you won't have any strange issues with any other program trying to read it, diff it, etc. The disadvantage is that it's harder to see the symbol in the code, of course.
与将字符直接放入源代码相比,这样做的优势在于您的源代码保持“纯 ASCII” - 这意味着您不会在任何其他程序尝试读取它、区分它等时遇到任何奇怪的问题。 缺点当然,代码中的符号更难看到。
回答by Jesse Pepper
The most common way seems to be to append a character of the form Chr(34)... 34 represents a double quote character. The character codes can be found from the windows program "charmap"... just windows/Run... and type charmap
最常见的方法似乎是附加一个 Chr(34)... 34 形式的字符表示一个双引号字符。字符代码可以从 windows 程序“charmap”中找到...只是 windows/Run... 并输入 charmap
回答by spacemonkeys
The ' character can be doubled up to allow it into a string e.g
' 字符可以加倍以使其成为字符串,例如
lSQLSTatement = "Select * from temp where name = 'fred''s'"
Will search for all records where name = fred's
将搜索 name = fred's 的所有记录
回答by RS Conley
If you are passing strings to be processed as SQL statement try doubling the characters for example.
例如,如果您要传递要作为 SQL 语句处理的字符串,请尝试将字符加倍。
"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "
The '' double works with the other special characters as well.
'' double 也适用于其他特殊字符。
回答by piratepops
Chr() is probably the most popular. ChrW() can be used if you want to generate unicode characters The ControlChars class contains some special and 'invisible' characters, plus the quote - for example, ControlChars.Quote
Chr() 可能是最受欢迎的。如果要生成 unicode 字符,可以使用 ChrW() ControlChars 类包含一些特殊和“不可见”字符,以及引号 - 例如,ControlChars.Quote
回答by Frederick The Fool
Three points:
三点:
1) The example characters you've given are not special characters. They're directly available on your keyboard. Just press the corresponding key.
1)您给出的示例字符不是特殊字符。它们可直接在您的键盘上使用。只需按下相应的键。
2) To type characters that don't have a corresponding key on the keyboard, use this:
2) 要键入键盘上没有相应键的字符,请使用以下命令:
Alt+ (the ASCII code number of the special character)
Alt+ (特殊字符的ASCII码号)
For example, to type ?, press Altand key in 168, which is the ASCII code for that special character.
例如,要键入 ?,请按Alt并键入168,这是该特殊字符的 ASCII 代码。
You can use this method to type a special character in practically anyprogram not just a VB.Net text editor.
您可以使用此方法在几乎任何程序中键入特殊字符,而不仅仅是 VB.Net 文本编辑器。
3) What you probably looking for is what is called 'escaping' characters in a string. In your SQL query string, just place a \before each of those characters. That should do.
3)您可能正在寻找的是字符串中所谓的“转义”字符。在您的 SQL 查询字符串中,只需在每个字符前放置一个\。那应该做。