SQL Insert problems

B

barry

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
 
J

Jon Skeet [C# MVP]

barry said:
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.
 
J

Jack Jackson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top