将DataTable更新到数据库一法

将DataTable更新到数据库一法

 

将DataTable或DataSet更新到后台数据库,一般使用SqlDataAdapter加SqlCommandBuilder完成,下面这过程代码,是没有用上述方式,直接提取数据集中更新部分,通过语句直接更新到数据库的,使用时有许多方便之处,而且,此法还可以为关 联表查询中的更新完成提交 . 如何使用,依个人偏好啦。

Public Function DtSave(ByVal sql As String, ByVal udt As DataTable, ByVal name As String, ByVal keys As Integer) As Boolean
       '保存表数据
       Dim Tdt As New DataTable
       Dim TJcm As New SqlCommand
       TJcm.CommandType = CommandType.Text
       TJcm.Connection = hyCN
       Dim Ri As Integer
       Dim Ci As Integer
       Dim Su As String
       Dim Tu As String
       Dim Tf As String
       Dim Tv As String
       Dim Tw As String
       Dim Ts As String
       Dim Ttype As String
       Ts = ""
       Try
           '删除?
           Tdt = udt.GetChanges(DataRowState.Deleted)
           If Not IsNothing(Tdt) Then
               Tdt.RejectChanges()
               With Tdt
                   Su = "DELETE FROM [" & name & "] "
                   For Ri = 0 To .Rows.Count - 1
                       Tw = "WHERE "
                       For Ci = 0 To keys - 1
                           Tw = Tw & "[" & .Columns(Ci).ColumnName & "]='" & .Rows(Ri).Item(Ci) & "' AND "
                       Next
                       Tw = Left(Tw, Len(Tw) - 4)
                       Tu = Su & Tw
                       Ts = Ts & Tu & " " & Chr(10)
                   Next
               End With
           End If

           '追加
           Tdt = udt.GetChanges(DataRowState.Added)
           If Not IsNothing(Tdt) Then
               With Tdt
                   Su = "INSERT INTO [" & name & "] "
                   Tf = ""
                   For Ci = 0 To .Columns.Count - 1
                       Tf = Tf & "[" & .Columns(Ci).ColumnName & "],"
                   Next
                   Tf = Left(Tf, Len(Tf) - 1)
                   For Ri = 0 To .Rows.Count - 1
                       Tv = " VALUES("
                       For Ci = 0 To .Columns.Count - 1
                           Select Case .Columns(Ci).DataType.Name
                               Case "String", "Char"
                                   Tv = Tv & "'" & .Rows(Ri).Item(Ci) & "',"
                               Case "DateTime"
                                   Tv = Tv & "'" & Format(.Rows(Ri).Item(Ci), "yyyy-MM-dd") & "',"
                               Case "Boolean"
                                   Tv = Tv & IIf(IIf(.Rows(Ri).IsNull(Ci), 0, .Rows(Ri).Item(Ci)), 1, 0) & ","
                               Case "Decimal", "Double", "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Byte", "SByte"
                                   Tv = Tv & IIf(.Rows(Ri).IsNull(Ci), 0, .Rows(Ri).Item(Ci)) & ","
                           End Select
                       Next
                       Tv = Left(Tv, Len(Tv) - 1) & ")"
                       Tu = Su & "(" & Tf & ")" & Tv
                       Ts = Ts & Tu & " " & Chr(10)
                       'TJcm.ExecuteNonQuery()
                   Next
               End With
           End If

           '更改
           Tdt = udt.GetChanges(DataRowState.Modified)
           If Not IsNothing(Tdt) Then
               With Tdt
                   Su = "UPDATE [" & name & "] SET "
                   Tf = ""
                   For Ri = 0 To .Rows.Count - 1
                       Tv = ""
                       Tw = " WHERE "
                       For Ci = 0 To keys - 1
                           Tw = Tw & " [" & .Columns(Ci).ColumnName & "]='" & .Rows(Ri).Item(Ci) & "' AND "
                       Next
                       Tw = Left(Tw, Len(Tw) - 4)
                       For Ci = keys To .Columns.Count - 1
                           Tf = " [" & .Columns(Ci).ColumnName & "]="
                           Ttype = .Columns(Ci).DataType.Name
                           Select Case Ttype
                               Case "String", "Char"
                                   Tv = Tv & Tf & "'" & .Rows(Ri).Item(Ci) & "',"
                               Case "DateTime"
                                   Tv = Tv & Tf & "'" & Format(.Rows(Ri).Item(Ci), "yyyy-MM-dd") & "',"
                               Case "Boolean"
                                   Tv = Tv & Tf & IIf(IIf(.Rows(Ri).IsNull(Ci), 0, .Rows(Ri).Item(Ci)), 1, 0) & ","
                               Case "Decimal", "Double", "Int16", "Int32", "Int64", "UInt16", "UInt32", "UInt64", "Single", "Byte", "SByte"
                                   Tv = Tv & Tf & IIf(.Rows(Ri).IsNull(Ci), 0, .Rows(Ri).Item(Ci)) & ","
                           End Select
                       Next
                       Tv = Left(Tv, Len(Tv) - 1)
                       Tu = Su & Tv & Tw
                       Ts = Ts & Tu & " " & Chr(10)
                   Next
               End With
           End If
           TJcm.CommandText = Ts
           TJcm.ExecuteNonQuery()
           hyDB.DBScalar("EXEC PMLTJ '" & name & "'")
           Return True
       Catch ex As SqlException
           Throw ex
           Return False
       Catch ey As Exception
           Throw ey
           Return False
       Finally
           TJcm.Dispose()
       End Try
   End Function

你可能感兴趣的:(Datatable)