sql server - SQL insert parameter value bit or datetime getting error syntax near @variablename -
ok i'm having connection sql server. have spent couple of hours looking through questions asked , have tried pretty everything. string.format query , have come conclusion need parametrize query multiple comments. have tested nvarchar type variable , works. problem have when add types bit , datetime. don't know 1 problem. bit 1 since error message :
incorrect syntax near @afficher
which of bit parameter
here code :
using(sqlconnection conn = new sqlconnection(connexion)) { conn.open(); sqlcommand cmd = new sqlcommand( "insert actualite (titre, sous_titre, contenu, image, vignette, video, date_publication, afficher) " + "values (@titre, @sous_titre, @contenu, @image, @vignette, @video, @date_publication, @afficher", conn); cmd.parameters.add(new sqlparameter("@titre", "ceci est un test")); cmd.parameters.add(new sqlparameter("@sous_titre", "ceci est un test")); cmd.parameters.add(new sqlparameter("@contenu", "ceci est un test, ça l'air à marcher")); cmd.parameters.add(new sqlparameter("@image", "minecraft.jpg")); cmd.parameters.add(new sqlparameter("@vignette", "vminecraft.jpg")); cmd.parameters.add(new sqlparameter("@video", "null")); cmd.parameters.add(new sqlparameter("@date_publication", sqldbtype.datetime)).value = datetime.now; cmd.parameters.add(new sqlparameter("@afficher", sqldbtype.bit)).value = 1; cmd.executenonquery(); }
i have tried using 1 , true parameter value , still error. head explode...
your insert statement missing closing parenthesis @ end of values clause.
also, if intended video
column contain null
after insert statement, should omit columns list , variables list, code should this:
using(sqlconnection conn = new sqlconnection(connexion)) { conn.open(); sqlcommand cmd = new sqlcommand( "insert actualite (titre, sous_titre, contenu, image, vignette, date_publication, afficher) " + "values (@titre, @sous_titre, @contenu, @image, @vignette, @date_publication, @afficher)", conn); cmd.parameters.add(new sqlparameter("@titre", "ceci est un test")); cmd.parameters.add(new sqlparameter("@sous_titre", "ceci est un test")); cmd.parameters.add(new sqlparameter("@contenu", "ceci est un test, ça l'air à marcher")); cmd.parameters.add(new sqlparameter("@image", "minecraft.jpg")); cmd.parameters.add(new sqlparameter("@vignette", "vminecraft.jpg")); cmd.parameters.add(new sqlparameter("@date_publication", sqldbtype.datetime)).value = datetime.now; cmd.parameters.add(new sqlparameter("@afficher", sqldbtype.bit)).value = 1; cmd.executenonquery(); }
Comments
Post a Comment