Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim fName As String = "FIL0001"
Dim DV2 As New DataView(DT.Table)
DV2.RowStateFilter = DataViewRowState.ModifiedCurrent
Dim drv As DataRowView
Dim c1 As New MISAccess("c:\Database\" & fName & ".dbf")
Dim strsql As String = ""
strsql = "update " & fName & " set mark = @P1 where code = @P2"
Dim TX As OleDbTransaction
Dim Cn As New OleDbConnection(c1.Strcon)
Dim Cmd As New OleDbCommand(strsql, Cn)
c1.CreateParam(Cmd, "MN")
Cn.Open()
TX = Cn.BeginTransaction
Cmd.Transaction = TX
Dim Fail As Boolean = False
For Each drv In DV2
Cmd.Parameters("@P1").Value = drv("mark")
Cmd.Parameters("@P2").Value = drv("code")
Try
Cmd.ExecuteNonQuery()
Catch
Fail = True
Exit For
End Try
Next
If Fail = True Then
TX.Rollback()
Label1.Text = "ไม่สามารถ Update ลงฐานข้อมูลได้ อาจมีข้อมูลซ้ำ"
Else
TX.Commit()
Label1.Text = "Update ลงฐานข้อมูลเรียบร้อยแล้ว"
DT.Table.AcceptChanges()
End If
Label1.Visible = True
Cn.Close()
grdDTS2.CurrentPageIndex = 0
grdDTS2.EditItemIndex = -1
Me.MyGridBind()
End Sub
Public Sub CreateParam(ByRef Cmd As OleDbCommand, ByVal StrType As String)
'T:Text, M:Memo, Y:Currency, D:Datetime, I:Integer, S:Single, B:Boolean
Dim i As Integer
Dim j As String
For i = 1 To Len(StrType)
j = UCase(Mid(StrType, i, 1))
Dim P1 As New OleDbParameter()
P1.ParameterName = "@P" & i
Select Case j
Case "T"
P1.OleDbType = OleDbType.VarChar
Case "M"
P1.OleDbType = OleDbType.LongVarChar
Case "Y"
P1.OleDbType = OleDbType.Currency
Case "M"
P1.OleDbType = OleDbType.Char
Case "N"
P1.OleDbType = OleDbType.Numeric
End Select
Cmd.Parameters.Add(P1)
Next
End Sub