user279521
user279521

Reputation: 4807

Apostrophe issue when writing a string to SQL Server in VB.net

I am having issues writing this string to my SQL 200 database table:

Conversion from string "" to type 'Integer' is not valid.

How can I ensure that my vb.net app writes the above string exactly as it appears, to the field in the table ?

Upvotes: 0

Views: 3110

Answers (2)

Alain
Alain

Reputation: 27220

I believe the only rules for escaping characters are:

  1. Use two quotes for every one displayed
  2. Escape wildcard characters (_ and %) with a backslash

So you should easily be able to insert any string you like by first doing 3 search and replaces on your string - replacing ' with '', replacing _ with \_, and replacing % with \%

Upvotes: 3

Rushyo
Rushyo

Reputation: 7604

I'd recommend using SqlCommand and SqlParameters, rather than raw SQL strings - for a variety of reasons. To escape characters in SQL, by default, I believe you simply need to double them up (eg. ' becomes '').

Upvotes: 4

Related Questions