c# - Cannot insert data into mbf database -


i have been trying insert data sql database last hours. 1 or other reason able connect database, no data inserted database. if run sql statement directly in database seem work. therefore, able conclude statement correct. furthermore, there no errors in runtime. have got following c# code:

//neither of these statements seem work. string sqlstatement = "insert dbo.eventtable (cola, colb, colc, cold, cole, colf, colg, colh, coli) values (@a,@b,@c,@d,@e,@f,@g,@h,@i)"; string altsqlstatement = "insert dbo.eventtable (cola, colb, colc, cold, cole, colf, colg, colh, coli) values (@a,@b,@c,@d,@e,@f,@g,@h,@i)";      foreach (datarow row in importdata.rows)     {         using (sqlconnection conn = new sqlconnection(form1.properties.settings.default.showconnectionstring))         {             using (sqlcommand insertcommand = new sqlcommand())             {                 insertcommand.connection = conn;                 insertcommand.commandtext = sqlstatement;                 insertcommand.commandtype = commandtype.text;                  insertcommand.parameters.addwithvalue("@a", row["cue"].tostring());                 insertcommand.parameters.addwithvalue("@b", row["hh"].tostring());                 insertcommand.parameters.addwithvalue("@c", row["mm"].tostring());                 insertcommand.parameters.addwithvalue("@d", row["ss"].tostring());                 insertcommand.parameters.addwithvalue("@e", row["ff"].tostring());                 insertcommand.parameters.addwithvalue("@f", row["addr"].tostring());                 insertcommand.parameters.addwithvalue("@g", row["event description"].tostring());                 insertcommand.parameters.addwithvalue("@h", row["cal"].tostring());                 insertcommand.parameters.addwithvalue("@i", row["pft"].tostring());                  try                 {                     conn.open();                     int _affected = insertcommand.executenonquery();                 }                 catch(sqlexception e)                 {                     // exception                  }             }         }    } 

if change connection parameters false, error occurs, seems correct.

any appreciated.

thanks!

alex

try using function template, big difference is opening connection before creating command. i've not seen done way have setup. should opening connection outside of loop, not in loop. why open , close repeatedly; foreach should inside inner 'using'

public void executequery(string query, dictionary<string, object> parameters) { using (sqlconnection conn = new sqlconnection(this.connectionstring)) {     conn.open();      using (sqlcommand cmd = conn.createcommand())     {         cmd.commandtext = query;          if (parameters != null)         {             foreach (string parameter in parameters.keys)             {                 cmd.parameters.addwithvalue(parameter, parameters[parameter]);             }         }          cmd.executenonquery();     } } } 

Comments