数据表插入触发器

Datatable insert trigger

我想在数据集 table 上实现 sql 插入触发器。

我的应用程序有两个数据集 tables:

-- Table: Artikli -- IDB - int,autoincrement Sifra - int, primary key Naziv - string Cena - double PS - string

-- Table: PodArtikli -- IDB - int,autoincrement,primary key Sifra - int Naziv - string Cena - double Kolicina - int Ukupno - computed column (Cena*Kolicina) Pakovanje - double Jed.Mere - string PLU - string, unique Cena_Po_Meri - computed column (1000/Pakovanje * Cena)

这些 table 通过外键约束相关,其中父 table 是 Artikli 并且子 table 是 PodArtikli 在列 Sifra. 我想,当一个新行添加到 Artikli 时,自动在 PodArtikli table 中添加新行 Sifra,Naziv,并从添加的 Cena 值中添加新行Artikli table.

中的行

数据集 tables 中的数据显示在 DataGridView 中。 在按钮 btnizmene 的点击事件中,我有以下代码:

Dim novirow As DataRow = dspetrovac.Artikli.NewRow
novirow("Sifra") = grdpodaci.Item(1, grdpodaci.CurrentRow.Index).Value
novirow("Naziv") = grdpodaci.Item(2, grdpodaci.CurrentRow.Index).Value
novirow("Cena") = grdpodaci.Item(3, grdpodaci.CurrentRow.Index).Value
novirow("PS") = grdpodaci.Item(4, grdpodaci.CurrentRow.Index).Value

您不能在 .NET 中编写触发器 DataTable 但您可以使用 DataTable 事件实现类似的功能。例如,您可以获取 RowChanging 事件并获取更改行,查看是否添加了行,然后使用该行中的数据将新行插入子 table

Private Sub Row_Changing(ByVal sender As Object, ByVal e As DataRowChangeEventArgs)
    If e.Action = DataRowAction.Add Then 
        Dim newChild as DataRow = childTable.NewRow()
        ' Get new row fr here and insert values 
        newChild("field") = e.Row("field")
        . . . . . . . 
        childTable.Rows.Add(newChild)
    End If
 End Sub