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

Re: SQL Insert problems by Jon

Jon
Sat Nov 24 01:12:55 PST 2007

barry <someone@somewhere.com> wrote:
> 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 ;)

> 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@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Re: SQL Insert problems by Jack

Jack
Sat Nov 24 08:29:13 PST 2007

On Sat, 24 Nov 2007 14:17:08 +0530, "barry" <someone@somewhere.com>
wrote:

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