Mysql DataAdapter更新方法 - 不保存更改

时间:2016-01-11 19:34:52

标签: mysql vb.net winforms datagridview

我在vb.net中使用MySQL Datagridview显示正确,一切正常,当我尝试使用更新(方法)更新数据库中的更改时出现问题 默认情况下,数据在内存中更新“已删除ID =”2“的学生 但是在数据库中没有更新,我正在阅读文档并且应该使用更新 datosAlumnos.Update(ds,“alumnos”)

Imports MySql.Data.MySqlClient
    Public Class Form1
        Dim con As New MySqlConnection
        Dim stringCon As String = "server=localhost; user id=root; password=; database=centroeducativo"
        Dim listViewAlumnos As New ListBox()

        Dim ds As New DataSet()
        Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load
            Try
                con.ConnectionString = stringCon
                con.Open()
                Dim datosTablas As New MySqlDataAdapter("SHOW TABLES", con)
                Dim datosAlumnos As New MySqlDataAdapter("SELECT * FROM alumnos ORDER BY Nombre", con)
                Dim datosAsignaturas As New MySqlDataAdapter("SELECT * FROM asignaturas", con)
                Dim datosMatriculas As New MySqlDataAdapter("SELECT * FROM matriculas", con)

                datosTablas.Fill(ds, "tablas")
                datosAlumnos.Fill(ds, "alumnos")
                datosAsignaturas.Fill(ds, "asignaturas")
                datosMatriculas.Fill(ds, "matriculas")

                con.Close()

                Dim tabla As DataTable
                tabla = ds.Tables("tablas")
                Dim fila As DataRow
                Me.ListBox1.Items.Clear()
                For Each fila In tabla.Rows

                    Me.ListBox1.Items.Add(fila.Item("Tables_in_centroeducativo"))
                Next
                Dim filaBorrada As DataRow() = ds.Tables("alumnos").Select("id=2")
                filaBorrada(0).Delete()
                datosAlumnos.Update(ds, "alumnos")
            Catch ex As Exception

            End Try
        End Sub


    Private Sub formularioTabla(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim curItem As String = ListBox1.SelectedItem.ToString()
        Select Case curItem
            Case "alumnos"
                FormularioAlumno()
            Case "asignaturas"
                FormularioAsignaturas()
            Case "matriculas"
                FormularioMatriculas()
            Case Else
                MsgBox("none")
        End Select
    End Sub

    Private Sub FormularioAlumno()
        Panel1.Controls.Clear()
        Dim dv As DataView = ds.Tables("alumnos").DefaultView
        con.Close()
        Dim DataGridView As New DataGridView()
        Panel1.Controls.Add(DataGridView)
        DataGridView.AutoSize = True
        DataGridView.DataSource = dv
        DataGridView.Columns("id").Visible = False
    End Sub
End Class

1 个答案:

答案 0 :(得分:2)

您似乎忘了设置InsertCommandUpdateCommand和特别Deletecommand

要使数据适配器更新数据,它应具有这些命令。您可以手动或使用MySqlCommandBuilder设置这些命令。

Dim myConn As New MySqlConnection("Connection String")
Dim myDataAdapter As New MySqlDataAdapter()
myDataAdapter.SelectCommand = New MySqlCommand("Select Query", myConn)
Dim myCommandBuilder As MySqlCommandBuilder = New MySqlCommandBuilder(myDataAdapter)