2016-02-26 68 views
2

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

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

-- 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)

其中父表是Artikli和子表是PodArtikliSifra列,这些表是通过外键约束相关联。 我想,当Artikli中新增一行时,会自动在PodArtikli表中添加新行,其中Sifra,NazivCena的值来自于Artikli表中添加的行。

数据集表中的数据显示在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 
+0

触发器通常实现在数据库中,虽然数据表呢有一些事件。如果DGV绑定到数据源,则不必手动将数据传送到newRow项目 – Plutonix

+0

我也试过TableNewRow事件,但没有任何结果。 – Gruja82

回答

0

你不能写一个触发器在.NET DataTable但可以使用DataTable事件实现类似的东西。例如,你可以采取RowChanging事件,并采取改变行,看看是否被添加行,然后从该行使用的数据中插入新行插入子表

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 
+0

thnx的想法。而不是Row_Changing,因为在Row_Changing事件行中,实际上还没有添加Row_Changed,并且引发了异常InvalidConstraintException,表示在父表中不存在子表中的'Sifra'列。非常感谢你。 – Gruja82

+0

@ Gruja82这很好。这正是我的尝试 - 引导你走向正确的方向。 'rowchanged'或'rowChanging' - 只是技术性,您需要根据您的软件构建和执行流程进行锻炼。请享用。 –