asp.net mvc - how to insert multiple dynamic row value into the database -


 public void create(account_detail c, int jobcard_id)         {             sqlconnection con = new sqlconnection(@"data source =(localdb)\v11.0;attachdbfilename=c:\users\wattabyte inc\documents\carinfo.mdf;integrated security=true;");             sqlcommand cmd = new sqlcommand();             cmd.commandtype = commandtype.text;             cmd.connection = con;             con.open();             foreach (var details in c.data)             {                 cmd.commandtext = "insert child_detail values ('" + jobcard_id + "','" + details.completed_by + "','" + details.reporting_time + "','" + details.cost_activity + "','" + details.spared_part_used + "');";                 cmd.executenonquery();             }          }           

i using code taking single value want save multiple values database ?

try like:

string additionaltext = string.empty; bool needcomma = false;  foreach (var details in c.data) {     if (needcomma)         additionaltext += ", ";     else         needcomma = true;      additionaltext += "('" + jobcard_id + "','" + details.completed_by + "','" + details.reporting_time + "','" + details.cost_activity + "','" + details.spared_part_used + "')"; }  cmd.commandtext = "insert child_detail values " + additionaltext + ";"; cmd.executenonquery(); 

basic idea - prepare string of teh command in foreach loop, execute command once.


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -