程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> .NET實例教程 >> Ds批量更新,帶有事務

Ds批量更新,帶有事務

編輯:.NET實例教程

    ''**********************************************************************
    ''* 函數名稱: UpdateDs
    ''* 函數描述: 更新一個DATASET,可以更新多個表,如果有一個出錯,要全部回滾事務.
    ''* 參數列表: arrSql:輸入DS 表格的查詢字符串數組,從下標為0處開始執行SQL語句。
    ''*           Ds:傳入要修改的DS
    ''*           arrTableName:DS中的表名,
    ''* 返 回 值: x=更新後的DS
    ''* 作        者: Itc-Lsg
    ''* 創建日期: 2007-12-22
    ''**********************************************************************
    Public Function UpdateDs(ByVal arrSql() As String, ByRef Ds As DataSet, ByVal arrTableName() As String) As DataSet


        Dim sqlcon As New SqlClIEnt.SqlConnection(Me.ConnectionString)
        Dim s As SqlClIEnt.SqlCommandBuilder
        Dim da As SqlClIEnt.SqlDataAdapter
        Dim sTableName As String = ""
        Dim strSql As String = ""
        Dim nArrLen As Integer = 0
        Dim i As Integer = 0
        Dim tbChange As New DataTable
        Dim Trans As Data.SqlClIEnt.SqlTransaction
        nArrLen = arrSql.Length
        If Not sqlcon.State = ConnectionState.Open Then
            sqlcon.Open()
        End If

        Try
            Trans = sqlcon.BeginTransaction
            For i = 0 To nArrLen - 1
       &n

$False$

bsp;        strSql = arrSql(i).Trim()
                sTableName = arrTableName(i)
                da = New SqlClIEnt.SqlDataAdapter(strSql, sqlcon)
                s = New SqlClIEnt.SqlCommandBuilder(da)


                If sTableName.Trim() = "" Then
                    tbChange = CType(Ds.Tables(0).GetChanges, DataTable)
                Else
                    tbChange = CType(Ds.Tables(sTableName).GetChanges, DataTable)
                End If
                da.SelectCommand.Transaction = Trans
                If Not tbChange Is Nothing Then
                    If sTableName.Trim() = "" Then
                        da.Update(Ds)
                        Ds.Tables(sTableName).Clear()
                        da.Fill(Ds)
                    Else
             ;           da.Update(Ds, sTableName)
                        Ds.Tables(sTableName).Clear()
                        da.Fill(Ds, sTableName)
                    End If
                    Ds.Merge(tbChange)

                End If
            Next
            Ds.AcceptChanges()

            Trans.Commit()

            Return Ds
        Catch ex As Exception
            Throw ThrowErr(ex)
            Trans.Rollback()
        Finally
            sqlcon.Close()
            sqlcon.Dispose()

        End Try
    End Function 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved