c# - Sql command for finding IDENT_CURRENT -


i have got find last number inserted identity column "id_k" table klient. how should improve code?

thanks in advance.

  sqlcommand comm = new sqlcommand("ident_current klient", spojeni);           //  sqlcommand comm = new sqlcommand("select max (id_k) klient", spojeni);             spojeni.open();             int max = (int)comm.executescalar();             spojeni.close();               foreach (datagridviewrow row in dtg_ksluzby.rows)             {                 if (convert.toboolean(row.cells[3].value) == true)                  {                     sqlcommand prikaz2 = new sqlcommand("insert klisluz(text,pocet,akce,subkey) values(@val1,@val2,@val3,@val4) ", spojeni);                     prikaz2.parameters.addwithvalue("@val1", row.cells["text"].value);                     prikaz2.parameters.addwithvalue("@val2", row.cells["pocet"].value);                     prikaz2.parameters.addwithvalue("@val3", row.cells["akce"].value);                     prikaz2.parameters.addwithvalue("@val4", max + 1);                     spojeni.open();                     prikaz2.executenonquery();                     spojeni.close();                 }             } 

the correct syntax ident_current is

 sqlcommand comm = new sqlcommand("select ident_current ('klient')", spojeni); 

keep in mind ident_current defined numeric(38,0), think best datatype use return value executescalar decimal

 decimal lastvalue = (decimal)comm.executescalar(); 

or, if values not big use

 int lastvalue = convert.toint32(comm.executescalar()); 

however command, max(k_id) suffers same problem. if different connection inserts new record in table 'klient' between call , call insert table 'klisluz' wrong number last identity value inserted. if there kind of relationship between 'klient' , 'klisluz' should explain because table name , column names not clear.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -