如果由.NET MySqlDataAdapter更新/插入,表触发器不会运行并且不会采用默认值

时间:2017-07-30 12:09:30

标签: mysql vb.net datagridview mysql-python

我正在使用MySql数据库,在表'tbl_person'处有一个触发器(修改后的更新时间戳)。此触发器工作正常,例如。使用MS ACCESS作为前端。但是,如果我使用DatagridView VB.NET,它使用绑定源到MySqlAdpater,那么adapater的更新方法正在以某种方式运行,既不运行触发器也不使用默认值。

我想知道是什么原因,如何防止这种情况,是否有更好的方法将数据网格视图与MySQl结合使用?也许有人可以给我一些提示。

如果您想尝试这样做:可以轻松复制代码,只需更改连接和表名。

此代码来自MSDN:https://msdn.microsoft.com/de-de/library/fbk67b6z(v=vs.110).aspx

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports MySql.Data.MySqlClient

Public Class Form2
Inherits System.Windows.Forms.Form

Private dataGridView1 As New DataGridView()
Private bindingSource1 As New BindingSource()
Private dataAdapter As New MySqlDataAdapter()
Private WithEvents reloadButton As New Button()
Private WithEvents submitButton As New Button()

Private connectionString As String = _
"server=127.0.0.1;user id=adm_test2;password=12init34;database=MyDb"

Private strSelect As String = "SELECT * FROM tbl_Person"

<STAThreadAttribute()> _
Public Shared Sub Main()
    Application.Run(New Form2())
End Sub

//' Initialize the form.
Public Sub New()

    Me.dataGridView1.Dock = DockStyle.Fill

    Me.reloadButton.Text = "reload"
    Me.submitButton.Text = "submit"

    Dim panel As New FlowLayoutPanel()
    panel.Dock = DockStyle.Top
    panel.AutoSize = True
    panel.Controls.AddRange(New Control() {Me.reloadButton, Me.submitButton})

    Me.Controls.AddRange(New Control() {Me.dataGridView1, panel})
    Me.Text = "DataGridView databinding and updating demo"

End Sub

Private Sub Form2_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Load

    //' Bind the DataGridView to the BindingSource
    //' and load the data from the database.
    Me.dataGridView1.DataSource = Me.bindingSource1
    GetData(strSelect)

End Sub

Private Sub reloadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles reloadButton.Click

    //' Reload the data from the database.
    GetData(Me.dataAdapter.SelectCommand.CommandText)

End Sub

Private Sub submitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles submitButton.Click

    //' Update the database with the user's changes.
    Me.dataAdapter.Update(CType(Me.bindingSource1.DataSource, DataTable))

End Sub

Private Sub GetData(ByVal selectCommand As String)

    Try

        Me.dataAdapter = New MySqlDataAdapter(selectCommand, connectionString)

        Dim commandBuilder As New MYSqlCommandBuilder(Me.dataAdapter)

        Dim MyDataTable As New DataTable()

        MyDataTable.Locale = System.Globalization.CultureInfo.InvariantCulture
        Me.dataAdapter.Fill(MyDataTable)
        Me.bindingSource1.DataSource = MyDataTable

        //' Resize the DataGridView columns to fit the newly loaded content.
        Me.dataGridView1.AutoResizeColumns( _
            DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
    Catch ex As Exception
    End Try

End Sub

End Class

0 个答案:

没有答案