如何在vb.net中将数据表更新到数据库

时间:2013-06-24 16:03:04

标签: datagridview

我正在尝试解决问题,但直到现在才失败

我的datagrid视图绑定到BindingSource但是当我从datagriview中删除某些行时,update(table)命令执行但不对我的数据库进行任何更改

我在这段代码中加载datagridview

Private Sub loadSalueUnits(ByVal itemName As String)

    table.Clear()
    If (itemName <> "0") Then
        ItemID = db.GetVal("select RawItemId from tblRawItem where RawItemName='" & itemName & "'")
        dsSaleUnit = db.GetData("SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & "")
    Else
        ItemID = "0"
    End If
    loadComboBox()

    'SaleUnit
    If (ItemID = "0") Then
        Try

            Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.bindingSource.DataSource = table
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            dgvSaleUnit.DataSource = Me.bindingSource
            SaleUnit.DataPropertyName = "Unit"
            UnitDifference.DataPropertyName = "UnitDifference"
            For i As Integer = 0 To table.Rows.Count - 1
                dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
            Next

            Me.dgvSaleUnit.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)
            dgvSaleUnit.Columns(2).Visible = False
            dgvSaleUnit.Columns(3).Visible = False
            dgvSaleUnit.Columns(4).Visible = False
            dgvSaleUnit.Columns(5).Visible = False
            dgvSaleUnit.Columns(6).Visible = False
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

    ElseIf dsSaleUnit.Tables(0).Rows.Count <> 0 Then

        Try

            Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits where RawItemId=" & ItemID & ""
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            Me.dataAdapter.Fill(table)
            Me.bindingSource.DataSource = table
            data.Locale = System.Globalization.CultureInfo.InvariantCulture
            dgvSaleUnit.DataSource = Me.bindingSource
            SaleUnit.DataPropertyName = "Unit"
            UnitDifference.DataPropertyName = "UnitDifference"
            salePrice.DataPropertyName = "SalePrice"
            For i As Integer = 0 To table.Rows.Count - 1
                dgvSaleUnit.Rows(i).Cells("UnitDifference").Value() = table.Rows.Item(i)(3)
                dgvSaleUnit.Rows(i).Cells("SaleUnit").Value() = table.Rows.Item(i)(2)
                dgvSaleUnit.Rows(i).Cells("SalePrice").Value() = table.Rows.Item(i)(4)
            Next

            Me.dgvSaleUnit.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCells)
            dgvSaleUnit.Columns(2).Visible = False
            dgvSaleUnit.Columns(3).Visible = False
            dgvSaleUnit.Columns(4).Visible = False
            dgvSaleUnit.Columns(5).Visible = False
            dgvSaleUnit.Columns(6).Visible = False

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End If

End Sub

并在此代码中删除datagridiew update中的行后

Dim objNetworksetting As New NetworkSetting
            Dim source As String = objNetworksetting.ReadConnectionXML()
            Dim connectionString As String = source
            Dim con As New OdbcConnection(connectionString)
            con.Open()

            Dim selectCommand As String = "SELECT * from tblItemSaleUnits"
            Me.dataAdapter = New OdbcDataAdapter(selectCommand, connectionString)
            Dim commandBuilder As New OdbcCommandBuilder(Me.dataAdapter)
            table.Locale = System.Globalization.CultureInfo.InvariantCulture
            dataAdapter.Fill(table)


            dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
            table = Me.bindingSource.DataSource
            table.AcceptChanges()
            Me.dataAdapter.Update(table)

它会执行,但不会对数据库进行任何更改

1 个答案:

答案 0 :(得分:0)

dataAdapter.UpdateCommand = commandBuilder.GetUpdateCommand()
        table = Me.bindingSource.DataSource
        table.AcceptChanges()
        Me.dataAdapter.Update(table)

通过这种方式,我们可以将数据表更新为数据库