Update multiple rows in access database using oled

2019-08-31 06:05发布

问题:

I am trying to update multiple rows in a access database using parameters. I already have to code to insert to a database, but I need same type of code to update the database. My update string looks like this Update tblitem set instock='value' where itemcode='value2' Here is my code to insert:

strSQL = "insert into tbltrans2 (transid,itemcode,itemname,qty,price,[total],btw) values ( ?,?,?,?,?,?,?)"
Using cn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Application.StartupPath + "\\POS.mdb"), _
    cmd As New OleDbCommand(strSQL, cn)

    cmd.Parameters.Add("?", OleDbType.VarChar).Value = txtTransId.Text
    cmd.Parameters.Add("?", OleDbType.VarChar, 10)
    cmd.Parameters.Add("?", OleDbType.VarChar, 50)
    cmd.Parameters.Add("?", OleDbType.Integer)
    cmd.Parameters.Add("?", OleDbType.Decimal)
    cmd.Parameters.Add("?", OleDbType.Decimal)
    cmd.Parameters.Add("?", OleDbType.VarChar, 50)

    cn.Open()
    For Each ls As ListViewItem In ListItems.Items
        cmd.Parameters(1).Value = ls.Tag
        cmd.Parameters(2).Value = ls.SubItems(0).Text
        cmd.Parameters(3).Value = Integer.Parse(ls.SubItems(1).Text)
        cmd.Parameters(4).Value = Decimal.Parse(ls.SubItems(2).Text)
        cmd.Parameters(5).Value = Decimal.Parse(ls.SubItems(3).Text)
        cmd.Parameters(6).Value = ls.SubItems(5).Text
        cmd.ExecuteNonQuery()
    Next ls
End Using

回答1:

You should, at the most basic level, be able to modify your sql string to indicate parameter placeholders (?) as you did in your Insert query, eg

Update tblitem set instock=? where itemcode=?

The concept beyond that, eg creating/setting the parameters, is essentially the same. Hope I'm understanding your problem properly. Good luck.



标签: sql vb.net oledb