使用oledb参数更新access数据库中的多行 [英] Update multiple rows in access database using oledb parameters

查看:80
本文介绍了使用oledb参数更新access数据库中的多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用参数更新访问数据库中的多行.我已经必须编写代码才能插入数据库,但我需要相同类型的代码来更新数据库.我的更新字符串看起来像这样 Update tblitem set instock='value' where itemcode='value2'这是我要插入的代码:

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

推荐答案

在最基本的层面上,您应该能够修改您的 sql 字符串以指示参数占位符 (?),就像您在插入查询中所做的那样,例如

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

更新 tblitem 集 instock=?其中 itemcode=?

除此之外的概念,例如创建/设置参数,本质上是相同的.希望我能正确理解你的问题.祝你好运.

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

这篇关于使用oledb参数更新access数据库中的多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆