tags:

views:

6209

answers:

6

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.

A: 

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.

2) To type characters that don't have a corresponding key on the keyboard, use this:

Alt + (the ASCII code number of the special character)

For example, to type ¿, press Alt and key in 168, which is the ASCII code for that special character.

You can use this method to type a special character in practically any program not just a VB.Net text editor.

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.

Frederick
ASCII runs up to 127; the ¿ character is in Latin-1. € isn't, that might be a bigger challenge.
MSalters
+2  A: 

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

Jesse Pepper
A: 

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.)

For example, for the Euro sign (U+20AC) you'd write:

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.

Jon Skeet
I can't write: Dim a as string = "you\I". Character (\) makes a mistake.
I'm not entirely sure how that comment is relevant to my answer... could you clarify?
Jon Skeet
Vb escape character is BASIC Convention of doubling the character. For example Dim TempS as String = "This has a "" in it". For character not on your keyboard Chr (and ChrW) is what been traditionally used.
RS Conley
@RS Conley: My point is that most C-based languages allow escaping of things other than quotes - e.g. \u20ac in this case.
Jon Skeet
RS Conley
Yeah, it's just a bit ugly - not so much for this, but for CR, LF, tab etc (where there are constants, I know, but it still means breaking out of the string).
Jon Skeet
I appreciate your point of view. Even back in the 80's when all there was BBS this was one of the points brought up between BASIC coders and C coders. In this case the C style is the more elegant.
RS Conley
+1  A: 

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

spacemonkeys
A: 

If you are passing strings to be processed as SQL statement try doubling the characters for example.

"SELECT * FROM MyRecords WHERE MyRecords.MyKeyField = ""With a "" Quote"" "

The '' double works with the other special characters as well.

RS Conley
A: 

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

piratepops