Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query: Double quote or apostrophe ?? 2

Status
Not open for further replies.

SebFr

Programmer
May 28, 2006
12
US
Hi,

I'm executing the following query on a SQL Server 2005 database via a vb.net application:

"SELECT FullName, Email, State, Country, Phone, Fax " & _
"FROM Customers " & _
"WHERE company LIKE '" & nameCompany & "%'"

nameCompany is a variable in my program.

My problem is that sometimes the name of the company contains an apostrophe (for example: Wireless's) and I have an error (incorrect syntax).
I've tried to replace the apostrophes by double quotes but it's not working.

Can anyone have a solution please ?
Thank you

Seb
 
Think of a single apostrophe as a delimiter for field data, and 2 adjacent apostophes to mean the apostrophe character. By doubling the apostrophe, a single apostrophe will be sent to the database.

Then, do me a favor. Do a google search on 'SQL injection attack' and read the first couple of pages it returns. By converting your code to a command object, you will be protected from this type of attack.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I can't replace with 2 apostrophes, nameCompany is a variable. I recover this name from another database, I can't change it.
 
But, you are using vb.net to send this query to SQL Server, right? The vb.net code should be doubling the apostrophe.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
There are many string manipulation functions in vb. Just change the string before you call the sql.
 
Thank you ! I don't understand why i didn't think about it earlier ! :/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top