c# - mysql insert max + 1 error -


i have win form contain data grid, add rows , want insert row in database each row has own id wrote query , try do errors when try insert max id +1 in each row please me write query correctly.

here query:

for (int = 0; < datagridview1.rows.count; i++) {     oracleconnection cn = new oracleconnection(connectionstring);     string query =             "insert emp_hasm_det " +            "(maxid,empid,ghyab,tagmee3,gza) " +            "  (select 1 + coalesce((select max(maxid) emp_hasm_det)), 1),'" +             this.datagridview1.rows[i].cells[0].value + "','" +             this.datagridview1.rows[i].cells[1].value + "','" +             this.datagridview1.rows[i].cells[2].value + "','" +             this.datagridview1.rows[i].cells[3].value + "'";     oraclecommand cmd = new oraclecommand(query, cn);     cn.open();     cmd.executenonquery();     cn.close(); } 

a few thoughts...

  • i don't see values clause in sql, think may part of problem.
  • you tagged question mysql reference oracle connection in code sample... it?
  • you opening , closing connection every row. that's lot of overhead, open once, issue commands, close it.

although not directly related question, might consider reformatting code use string.format shown below. makes things little easier read. particularly because aren't appending single-quotes. put debug.writeline statement in code have , let know outputs... might make problem more obvious , allow better.

-- untested code follows --

string sqltemplate = "insert emp_hasm_det(maxid,empid,ghyab,tagmee3,gza) values ({0}, '{1}', '{2}', '{3}', '{4}')"; string sqlsubquery = "(select coalesce(max(maxid)+1, 1) emp_hasm_det)";  oracleconnection cn = new oracleconnection(connectionstring); cn.open(); (int = 0; < datagridview1.rows.count; i++) {   string query = string.format(sqltemplate,          sqlsubquery,         this.datagridview1.rows[i].cells[0].value,         this.datagridview1.rows[i].cells[1].value,         this.datagridview1.rows[i].cells[2].value,         this.datagridview1.rows[i].cells[3].value);    debug.writeline(query);    oraclecommand cmd = new oraclecommand(query, cn);   cmd.executenonquery(); } cn.close(); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -