Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Should we escape ' when building command text?
Message
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Bases de données
Titre:
Should we escape ' when building command text?
Versions des environnements
Environment:
ASP.NET
OS:
Windows XP
Database:
MS SQL Server
Divers
Thread ID:
01278630
Message ID:
01278630
Vues:
78
Hi everybody,

I'm looking into the existing code trying to figure out the problem. This code is supposed to create a recurring event, so it is supposed to insert a record into the Programs table and then multiple records into ProgramsTimes table. It is done directly through CommandText.

This is the code:

Command.CommandText = string.Format("INSERT INTO Programs (ProgName, ProgramDescription, " +
"Location, CoordinatorID) VALUES('{0}','{1}', '{2}',{3})\nSELECT @@IDENTITY", this.txtbName.Text,
this.txtbDescription.Text, this.txtbLocation.Text, CoID);

When I was testing this from the user interface I typed ' in the Description field.

I'm getting an error 'Incorrect syntax near 's. Unclosed quotation mark...'

So, my first question - is the above command correct or should it be changed?

My second question - how can I insert datetime when I'm writting a comment?

Also, is it a general practice to handle this problem with direct insert commands or there is a better way of doing it? There is a complex logic of calculating dates and times depending on the interface...

Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform