Insertar en la tabla de SQL Server no me da nada

Very frustrating one... I have tried many combinations of ', " and so on but my insert command just refreshing the page.

¿Qué estoy haciendo mal?

Simple two text fields form with button. Under button I have this:

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["przychodniaConnectionString1"].ConnectionString);
con.Open();

string cmdStr = "INSERT INTO specyfik(speNazwa, speIlosc) values ('" + speNazwa.Text + "', '" + speIlosc.Text + "')";
SqlCommand insertCmd = new SqlCommand(cmdStr, con);
con.Close();

Zero errors while compiling and when testing, it seems like refreshed page. Nothing appears in db.

preguntado el 10 de marzo de 12 a las 16:03

3 Respuestas

No necesitas llamar insertCmd.ExecuteNonQuery() ?

...
SqlCommand insertCmd = new SqlCommand(cmdStr, con); 
int row_affected = insertCmd.ExecuteNonQuery();
...

respondido 10 mar '12, 16:03

You need to execute your SqlCommand:

insertCmd.ExecuteNonQuery();

Also, you should look into parameterizing that query: http://www.codinghorror.com/blog/2005/04/give-me-parameterized-sql-or-give-me-death.html

respondido 10 mar '12, 16:03

I need some way to +1 again the parameterizing sentence. - damien_el_incrédulo

Thank you! A habbit from php I guess, because I thought that SqlCommand insertCmd = new SqlCommand(cmdStr, con); will do a job ;) - dargod

Will you like to make more improvements in your code using stored Proc and improvemnet in your code behind file ? Take a look at this answer...

https://stackoverflow.com/a/9595501/1209450

contestado el 23 de mayo de 17 a las 13:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.