SQL Insert problems

  • Thread starter Thread starter barry
  • Start date Start date
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
 
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.
 
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.
 
Back
Top