c# - Insert Double In Sql Server -


i using sql server 2008 , c#. i'm declaring variable of type double , want insert in database. have error:

data type conversion error varchar numeric.

how insert value of type double in sql server using c#?

this code:

double variable = 13.2;  con.open(); cmd=con.createcommand(); cmd.commandtext = "insert etudient (idetudient, [nom etudient], value) values('" + convert.toint16( text_id.text) + "' , '" + text_name.text + "','" + variable + "')"; cmd.executenonquery(); con.close();  messagebox.show("success"); 

since, variable of type double not string, should inserting variable in format:

" + variable + " , not '" + variable + "'

further, insert query goes this:

cmd.commandtext = "insert etudient (idetudient, [nom etudient], value)     values('" + convert.toint16( text_id.text) + "' , '" + text_name.text + "'," + variable + " )"; 

it infact, recommended use parameterized queries prevents sql injection attacks.

using (var con = new sqlconnection(connectionstring)) {     con.open();      using (var cmd = new sqlcommand(         @"insert etudient (idetudient, [nom etudient], value)           values (@id, @name, @variable)", con))     {         cmd.parameters.addwithvalue("@id", text_id.text);         cmd.parameters.addwithvalue("@name", text_name.text);         cmd.parameters.addwithvalue("@variable", variable);         cmd.executenonquery();     } } 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -