使用MS Access在VB.NET中更新查询不起作用

时间:2013-07-18 11:31:26

标签: vb.net ms-access-2010 auto-update

Imports System.Data.OleDb

Public Class Form1

这是我的连接线

Dim con As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|datadirectory|STORE.accdb")

Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
    con.Open()

    Dim cmd As New OleDbCommand("SELECT        [QTY OTHER], [HQTY PL3], [HQTY PL5], [TOTAL QTY] FROM(MATERIAL)WHERE        ([MAT CODE] = @a)", con)


    cmd.Parameters.AddWithValue("@a", TextBox1.Text)
    Dim dr As OleDbDataReader = cmd.ExecuteReader
    Dim qtyotherm, hqtypl3m, hqtypl5m, totalqtym As New Double
    Dim qtyotherr, rqtypl3r, rqtypl5r, qtyr As New Double
    Dim totalqtyfinal As New Double

    Dim cmdins As New OleDbCommand("INSERT INTO RECEPT ([MAT CODE], QTY, RATE, TAXES, [RQTY PL3], [RQTY PL5], [RQTY OTHER]) VALUES        (@b, @c, @d, @e, @f, @g, @h)", con)


    cmdins.Parameters.AddWithValue("@b", TextBox1.Text)
    cmdins.Parameters.AddWithValue("@c", TextBox2.Text)
    cmdins.Parameters.AddWithValue("@d", TextBox10.Text)
    cmdins.Parameters.AddWithValue("@e", TextBox9.Text)
    cmdins.Parameters.AddWithValue("@f", TextBox3.Text)
    cmdins.Parameters.AddWithValue("@g", TextBox4.Text)
    cmdins.Parameters.AddWithValue("@h", TextBox5.Text)
    Dim checkinsert As New Integer
    checkinsert = cmdins.ExecuteNonQuery

此执行非查询正常工作

    If checkinsert > 0 Then




        If dr.HasRows Then


            While dr.Read
                qtyotherm = dr(0)
                hqtypl3m = dr(1)
                hqtypl5m = dr(2)
                totalqtym = dr(3)
            End While

文字框显示了附加价

            totalqtyfinal = totalqtym + TextBox2.Text
            TextBox15.Text = totalqtyfinal



            MessageBox.Show("chk text box value")

问题从这里开始

            Dim cmdupdate As New OleDbCommand("UPDATE MATERIAL SET [TOTAL QTY] = @j  WHERE ([MAT CODE] = @i)", con)


            cmdupdate.Parameters.AddWithValue("@i", TextBox1.Text)
            cmdupdate.Parameters.AddWithValue("@j", TextBox15.Text)
            Dim chkupdate As New Integer
            chkupdate = cmdupdate.ExecuteNonQuery


            If chkupdate > 0 Then
                MessageBox.Show("update done   :)  ")

始终显示此声明信息

            Else
                MessageBox.Show("fail to update  :(")
            End If




            MessageBox.Show("quer 1 and 2 is successfully executed")

        Else
            MessageBox.Show("dr not working.")
        End If
    Else
        MessageBox.Show("insert query fail")
    End If
    con.Close()

End Sub

结束班

1 个答案:

答案 0 :(得分:1)

this answer。 OleDbCommand不支持命名参数。您需要以与SQL语句中相同的顺序添加参数。

在你的更新声明中,你有@j后跟@i,你要添加@i后跟@j。 OleDbCommand有效地将其视为?其次是 ?在SQL语句中,当您添加参数时,它只需添加参数1,然后添加参数2。