mysql - how to use Loop in c# insert query -
i'm student in vocational highschool , newbie programmer.. have problem using loop in insert query.. have 2 table, book , book_detail, wich link foreign key, , want save data both table, , problem every time add book, have textbox named copies_txt, idea want save same book_id , same location_id in book_detail table diffrent boookdetail_id as amount insert in copies_txt.. , achieve try using loop, gives me error
duplicate entry key primary
, here's full code
int copies = convert.toint32(copies_txt.text); int i; string constring = "datasource=localhost;port=3306;username=root;password=root"; string query = "insert simbada_perpustakaan.book(book_id,title,release_date,genre,author,copies,create_by,create_date) values ('" + this.book_id.text + "','" + this.title_txt.text + "','" + this.time.text + "','" + this.genre_txt.text + "','" + this.author_txt.text + "','" + this.copies_txt.text + "','" + this.username_lbl.text + "','" + datetime.now.tostring("yyyy/mm/dd/hh/mm/ss") + "') ; "; string query2 = "insert simbada_perpustakaan.book_detail(id_bookdetail,book_id,location_id) values('" + guid.newguid() + "','" + this.book_id.text + "','" + this.textbox1.text + "') on duplicate key update id_bookdetail=(id_bookdetail=('"+guid.newguid()+"'))"; mysqlconnection condatabase = new mysqlconnection(constring); mysqlcommand cmddatabase = new mysqlcommand(query, condatabase); mysqlcommand cmddatabase2 = new mysqlcommand(query2, condatabase); mysqldatareader myreader; try { condatabase.open(); myreader = cmddatabase.executereader(); condatabase.close(); (i = 0; <= copies; i++) { condatabase.open(); myreader = cmddatabase2.executereader(); condatabase.close(); } messagebox.show("saved"); while (myreader.read()) { } } catch (exception ex) { messagebox.show(ex.message); } can please point out mistake... before.
i think issue second query. move query inside loop , check. don't need data reader since it's select statement. can avoid multiple database open , close statement. try below code
int copies = convert.toint32(copies_txt.text); int i; string constring = "datasource=localhost;port=3306;username=root;password=root"; string query = "insert simbada_perpustakaan.book(book_id,title,release_date,genre,author,copies,create_by,create_date) values ('" + this.book_id.text + "','" + this.title_txt.text + "','" + this.time.text + "','" + this.genre_txt.text + "','" + this.author_txt.text + "','" + this.copies_txt.text + "','" + this.username_lbl.text + "','" + datetime.now.tostring("yyyy/mm/dd/hh/mm/ss") + "') ; "; mysqlconnection condatabase = new mysqlconnection(constring); mysqlcommand cmddatabase = new mysqlcommand(query, condatabase); mysqldatareader myreader; try { condatabase.open(); cmddatabase.executenonquery(); (i = 0; <= copies; i++) { string = guid.newguid().tostring(); string query2 = "insert simbada_perpustakaan.book_detail(id_bookdetail,book_id,location_id) values('" + + "','" + this.book_id.text + "','" + this.textbox1.text + "') on duplicate key update id_bookdetail=(id_bookdetail=('"+a+"'))"; mysqlcommand cmddatabase2 = new mysqlcommand(query2, condatabase); cmddatabase2.executenonquery(); } messagebox.show("saved"); condatabase.close(); } catch (exception ex) { messagebox.show(ex.message); } also try parameterized query. can avoid sql injection problem.
Comments
Post a Comment