Sunday 15 February 2015

mysql - How to Get the Auto-Increment Values after Insert Statement i n vb.net 2010 -



mysql - How to Get the Auto-Increment Values after Insert Statement i n vb.net 2010 -

i new in vb.net , mysql database, far practicing i'm stack in getting value of current info of attribute table auto increment after inserting query. here code:

imports mysql.data.mysqlclient seek dim dbcon new mysqlconnection public dbcom new mysqlcommand public sql string dim theid string ' variable auto increment id stored. dbcon.connectionstring = "server=localhost; uid=root; pwd=; database=test;" dbcon.open() sql = "insert tablethis values('" & name & "');" 'this produced auto increment value dbcom = new mysqlcommand(sql, dbcon) dbcom.executenonquery() msgbox("added!your id is: ", & theid) ' auto increment id display grab ex exception msgbox("error!") exit sub end seek

** table is:

------------------------------------- -- tablethis ---- ------------------------------------- -- id -- integer (auto_increment) - -- name -- char - -------------------------------------

thanks! sorry bad english.

the result of lastly auto increment field in connection obtained using

select last_insert_id()

to add together part current code need

using dbcon = new mysqlconnection("server=localhost; uid=root; pwd=; database=test;") using dbcom = new mysqlcommand("insert tablethis values(@name);select last_insert_id();", dbcon) dbcon.open() dbcom.parameters.addwithvalue("@name", name ) dim theid = convert.toint32(dbcom.executescalar()) msgbox("added!your id is: " & theid.tostring) end using end using

i have changed other things here:

first of all, should utilize parameterized query , not string concatenations (read sql injection understand why) second, have used using statement around disposable objects correctly dispose connection , command. third, have passed 2 commands separating them semicolon , retrieved resulting autoincrement using executescalar. notice autoincrement column of type integer , not string

mysql vb.net

No comments:

Post a Comment