数据未正确更新

时间:2013-12-20 17:16:17

标签: vb.net datagridview ms-access-2007

我有2个表单... Dress_Price用于显示数据表单数据库(MS Access 2007)的另一种表单,Edit_Dress用于编辑和更新数据库..

代码根据Edit_Dress格式的更改成功更新了数据..但是存在2个问题 -

  1. 更新后dgvCustomerDressPrice未刷新..

  2. 更新后会在记录中添加“空格”..

    更新价格值

    之前

    Dress_Name =“Tuxedo”Dress_Price =“150”

    更新后的价格值

    Dress_Name =“Tuxedo”Dress_Price =“250”

  3. 每次更新记录时,“空间”都会被添加...所以搜索功能因空间不能正常工作..

    Dress_Price上的

    代码: -

    Private Sub Dress_Price_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                    con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb"
                    con.Open()        
    
        dgvCustomerDressPrice()        
    End Sub 
    
    Private Sub dgvCustomerDressPrice() 
    
            Dim ds As New DataSet
            Dim dt As New DataTable
            ds.Tables.Add(dt)
            Dim da As New OleDb.OleDbDataAdapter
    
            da = New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress", con)
    
            da.Fill(dt)
    
            dgvDressPrice.DataSource = dt.DefaultView
    
            dgvDressPrice.SelectionMode = DataGridViewSelectionMode.FullRowSelect
            con.Close()
        End Sub
    
    Private Sub btnEditDress_Click(sender As Object, e As EventArgs) Handles btnEditDress.Click
    
            If dgvDressPrice.Rows.Count > 0 Then ' when user click a row, any query for database will based on Order_ID
    
                If dgvDressPrice.SelectedRows.Count > 0 Then
    
                    Dim intDressID As Integer = dgvDressPrice.SelectedRows(0).Cells("Dress_ID").Value 
    
    
                    Try
                        If Not con.State = ConnectionState.Open Then
                            con.Open()
                        End If
    
                        Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress WHERE Dress_ID =" & intDressID, con)
                        ' the record that will be edited is based on the Customer_ID of particular row clicked
    
                        Dim dt As New DataTable
    
                        da.Fill(dt)
    
                        Edit_Dress.txtDressID.Text = intDressID
                        Edit_Dress.txtDressName.Text = dt.Rows(0).Item("Dress_Name")
                        Edit_Dress.txtDressPrice.Text = dt.Rows(0).Item("Dress_Price")
                        ' pass the data from tbl_user into each represented field
    
                    Catch ex As Exception
                        MessageBox.Show("Failed to edit data ! System eror : " & ex.ToString, "Eror !", MessageBoxButtons.OK)
                    End Try
                End If
            End If
            Edit_Dress.Show()
        End Sub
    
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
            If Not con.State = ConnectionState.Open Then
                con.Open()
            End If
    
            Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM tbl_dress WHERE Dress_Name like '" & txtSearch.Text & "%' ", con)
    
            Dim dt As New DataTable
            da.Fill(dt)
    
            dgvCustomerDressPrice().DataSource = dt
    
            con.Close()
        End Sub
    
    Edit_Dress上的

    代码: -

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
            con = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\annonymous\Documents\Visual Studio 2012\Projects\TMS Final\TMS Final\db\db_TMS.accdb")
    
            Try
                If con.State = ConnectionState.Closed Then
                    con.Open()
                End If
    
                Dim intDressID As Integer
                intDressID = Convert.ToInt32(txtDressID.Text)
                intDressID = Integer.Parse(txtDressID.Text)
    
                Dim intDressPrice As Integer
                intDressPrice = Convert.ToInt32(txtDressPrice.Text)
                intDressPrice = Integer.Parse(txtDressPrice.Text)
    
                Dim query As String = "UPDATE tbl_dress SET Dress_Name = ' " & txtDressName.Text & " ' ,  Dress_Price = ' " & intDressPrice & " '  WHERE Dress_ID =  " & intDressID & "  "
    
                Dim cmd As New OleDb.OleDbCommand(query, con)
    
                cmd.ExecuteNonQuery()
    
                MessageBox.Show("Data updated !", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    
                Dress_Price.RefreshPriceList()
    
                con.Close()
    
                Me.Close()
            Catch ex As Exception
                MessageBox.Show("Failed to save into database ! System eror : " & ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End Sub
    

1 个答案:

答案 0 :(得分:2)

查看您的UPDATE方法,很清楚为什么每次都添加一个空格。你把它放在更新字符串中。

Dim query As String = "UPDATE tbl_dress SET Dress_Name = ' " & _
                                                          ^ here
                      txtDressName.Text & " ' ,  Dress_Price = ' " & _
                                           ^here                ^here
                      intDressPrice & " '  WHERE Dress_ID =  " & intDressID & "  "
                                       ^here

可以修复删除空格的简单错误的一部分,这不是创建更新命令的正确方法。您应该使用参数化查询

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    con = New OleDb.OleDbConnection(.....)
    Try
      If con.State = ConnectionState.Closed Then
         con.Open()
      End If

      Dim intDressID As Integer
      intDressID = Convert.ToInt32(txtDressID.Text)

      Dim intDressPrice As Integer
      intDressPrice = Convert.ToInt32(txtDressPrice.Text)

      Dim query As String = "UPDATE tbl_dress SET Dress_Name = ?, Dress_Price = ? " & _
                            "WHERE Dress_ID =  ?"
      Dim cmd As New OleDb.OleDbCommand(query, con)
      cmd.Parameters.AddWithValue("@p1", txtDressName.Text)
      cmd.Parameters.AddWithValue("@p2", intDressPrice)
      cmd.Parameters.AddWithValue("@p3", intDressID)
      cmd.ExecuteNonQuery()
      .....