HighTechTalks DotNet Forums  

SQL Insert problems

Dotnet Framework microsoft.public.dotnet.framework


Discuss SQL Insert problems in the Dotnet Framework forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
barry
 
Posts: n/a

Default SQL Insert problems - 11-24-2007 , 03:47 AM






Hi

Using the old style of inserting data in a table

str="Insert Into MyTable (Description, Test) Values (
'The Quick, Black Jump's, Over The Lazy Dog', 'Test'
)";

The above would fail since there are commas and single quote in the data.

I thought fill(ing) a Dataset and the using update would overcome the
problem, but it also has the same error, how does one solve such issues.

TIA
Barry



Reply With Quote
  #2  
Old   
Jon Skeet [C# MVP]
 
Posts: n/a

Default Re: SQL Insert problems - 11-24-2007 , 04:12 AM






barry <someone (AT) somewhere (DOT) com> wrote:
Quote:
Using the old style of inserting data in a table

str="Insert Into MyTable (Description, Test) Values (
'The Quick, Black Jump's, Over The Lazy Dog', 'Test'
)";

The above would fail since there are commas and single quote in the data.
It would fail an English grammar test too, but that's a different
matter

Quote:
I thought fill(ing) a Dataset and the using update would overcome the
problem, but it also has the same error, how does one solve such issues.
Use a parameterised SqlCommand instead of including the values in the
SQL statement itself.

--
Jon Skeet - <skeet (AT) pobox (DOT) com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Reply With Quote
  #3  
Old   
Jack Jackson
 
Posts: n/a

Default Re: SQL Insert problems - 11-24-2007 , 11:29 AM



On Sat, 24 Nov 2007 14:17:08 +0530, "barry" <someone (AT) somewhere (DOT) com>
wrote:

Quote:
Hi

Using the old style of inserting data in a table

str="Insert Into MyTable (Description, Test) Values (
'The Quick, Black Jump's, Over The Lazy Dog', 'Test'
)";

The above would fail since there are commas and single quote in the data.

I thought fill(ing) a Dataset and the using update would overcome the
problem, but it also has the same error, how does one solve such issues.

TIA
Barry
The single quote inside the first value causes a problem, but commas
do not. You have two choices. The easiest solution is to use
parameters for the values. The other way is to use whatever method
the database supports to enclose single quotes within a string. Often
that is to double the quotes, but it might vary by type of backend.


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.