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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Commas in a SQL statements

Status
Not open for further replies.

TheMaskedPencil

Programmer
Jan 18, 2005
6
CA
I need to know if it is possible to add a comma into a database field, that is from a string variable. Here is an example.

strString = "Here, there and everywhere."

"INSERT INTO table ([Field]) VALUES ('" & strString & "')"

Every time I run, I get an error message, the comma acts as a seperator, like when you are inserting multiple values. Is there anyway to fix it, or will I have to reformat the string and replace the comma with a different character, before I can save it to the database(Access 2000)

Please tell me there is a simple anwser.
 
Rather than building your insert statement dynamically, you need to look into using SQL Parameters.

Sweep
...if it works dont f*** with it
curse.gif
 
SQL Parameters? Where can I get information on that? I have been looking on Google and there hasn't been really a good explanation of it.
 
in VB6 i use the following

'" & Replace(Textbox.Text, "'", "''"

 
thread796-1104577

see last post

dvannoy this is the vb.net forum, allthough what you say will work even in .net.

Christiaan Baes
Belgium

I just like this --> [Wiggle] [Wiggle]
 
chrissie1,

thank you for pointing that out..I am aware of which forum I am in. But again, thank you for pointing that out.

 
dvannoy, don't provoke him, he bites. And it's only a matter of time until the freaky distracting sig wears off and he realises he's hungry.

But there is an excellent example of using parameters about 2/3s the way down in thread796-1104577 as Chrissie mentioned.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
There are many benefits to using parameterized SQL besides not having to mess around with single-quotes.

For example, your queries will run faster, because the database engine (assuming you're not using Access) will be able to store them in it's procedure cache for later use.

You'll also be protected against the SQLInjection attack, whereupon an attacker is able to execute arbitrary SQL against your database, and possibly run operating system commands.

Here's a good article & debate on the subject, plus you'll want to do a google for yourself.


Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top