如何在VB代码中编写特殊字符

时间:2022-09-13 09:01:30

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 statament(例如:('),(/),(&)),我不知道如何在我的VB.NET代码中编写它们。请帮我。谢谢。

6 个解决方案

#1


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” - 这意味着你不会有任何其他程序尝试阅读它,差异等等的任何奇怪问题。当然,在代码中看到符号更难。

#2


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

#3


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也与其他特殊字符一起使用。

#4


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的所有记录

#5


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 Alt and 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 any program 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查询字符串中,只需在每个字符前放置\。应该这样做。

#6


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

#1


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” - 这意味着你不会有任何其他程序尝试阅读它,差异等等的任何奇怪问题。当然,在代码中看到符号更难。

#2


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

#3


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也与其他特殊字符一起使用。

#4


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的所有记录

#5


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 Alt and 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 any program 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查询字符串中,只需在每个字符前放置\。应该这样做。

#6


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