sql - Cannot insert the data into the table C# visual studio 2013 -
i want write data local database table. when run code there no erros , when count rows after insert statement message box shows me row inserted. when close programm , in database there no new rows.
i'm using c# , visual studio 2013.
do know problem is?
thank you.
string connection = "data source=(localdb)\\v11.0;attachdbfilename=|datadirectory|\\datenbank.mdf;integrated security=true;connect timeout=30"; sqlconnection cnn = new sqlconnection(connection); cnn.open(); string query = "insert customer (id, name) values (@id, @name)"; sqlcommand command = new sqlcommand(query, cnn); command.parameters.addwithvalue("@id", 1); command.parameters.addwithvalue("@name", 'john'); sqldatareader reader; command.executenonquery(); query = "select count(id) customer"; command = new sqlcommand(query, cnn); reader = command.executereader(); while (reader.read()) { messagebox.show(reader[0].tostring()); } reader.close();
try this:
try { int rowsaffected = command.executenonquery(); if (0 < rowsaffected) messagebox.show("success!"); else messagebox.show("failed!"); } catch (sqlexception ex) { messagebox.show(ex.message); } { if (cnn.state == connectionstate.open) cnn.close(); } also refer: why saving changes database fails?
Comments
Post a Comment