vb.net - Trying to delete a record from my access database in visual basic studio 2010 -
private function createplayeradapter(byval playerdbconnection oledbconnection) oledbdataadapter
// initiating instances function
dim dataadapter oledbdataadapter = new oledbdataadapter() dim mycommand oledbcommand dim parameter oledbparameter
// establishing string tell delete record , how find record want. // playeridtextbox.text text on form populated database after selecting list of name (this works correctly) // connection open , directed correct place
dim sql string = "delete * players id ='" & cint(playeridtextbox.text) & "'" mycommand = new oledbcommand(sql, playerdbconnection) parameter = mycommand.parameters.add("id", oledbtype.char, 3, "id") parameter.sourceversion = datarowversion.original dataadapter.deletecommand = mycommand return dataadapter end function
// call function after executing button click. //listplayercombobox.text populated names , needs name fill playeridtextbox.text(works correctly)
private sub removeplayerbutton_click(sender system.object, e system.eventargs) handles removeplayerbutton.click if listplayercombobox.text = " " msgbox("please select player.") else me.createplayeradapter(playerdbconnection) end if end sub
// no errors occur. however, nothing done in database. please?
notes:
1)never leave oledbconnection open
. allow opened when need it. save lot of headaches later on. reasons why can found on following stackoverflow question.
2) there no reason return oledbdataadapter
if don't intend on using it.
3) use parameters correctly : see below example2
4) keep in mind there restricted keywords
in access
. luckely id
isn't one. restrictedkeywords can found here: keywords
i'm missing further points here. should free add em.
why not adjust function createplayeradapter
following:
1) without parameters
private sub createplayeradapter(byval playerdbconnection oledbconnection) dim mycommand oledbcommand dim sql string = "delete * players id =" & cint(playeridtextbox.text) mycommand = new oledbcommand(sql, playerdbconnection) playerdbconnection.open() mycommand.executenonquery() playerdbconnection.close() end sub
2) parameters
private sub createplayeradapter(byval playerdbconnection oledbconnection) dim mycommand oledbcommand dim sql string = "delete * players id = @playerid" mycommand = new oledbcommand(sql, playerdbconnection) dim param new oledb.oledbparameter(@playerid", cint(playeridtextbox.text)) mycommand.add(param) playerdbconnection.open() mycommand.executenonquery() playerdbconnection.close() end sub
the method executenonquery executes query
passed command on specified oledbconnection
, returns number of rows affected. more info here
Comments
Post a Comment