我想在数据集表上实现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
,子表为PodArtikli
column Sifra
。我希望将新行添加到中时Artikli
,使用来自动在PodArtikli
表中添加新行Sifra,Naziv
,并在表中添加新行中的Cena
值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
您不能在.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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句