SqlDataAdapter的不更新 [英] SqlDataAdapter Not Updating

查看:162
本文介绍了SqlDataAdapter的不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试更新 TableTwo 使用数据表使用内置 TableOne 。 表之间的关系是外交列名为 TableOneId TableTwo

我用下面的code样本,使这项工作: 执行批处理操作使用DataAdapters(MSDN)

数据表填充在另一个公共共享的功能。

我无法弄清楚什么是错的。没有错误消息的报道。该表显示,数据表装入数据。

数据表定义为:

 公共MyDataTable作为新数据表

公共共享子DefineDataTable()

    昏暗ErrorEmail作为新ErrorEmailMessageClass
    随着ErrorEmail
        尝试
            使用连接作为新的SqlConnection(My.Settings.MyDB)
                MyDataTable.Columns.Add(ID,Type.GetType(System.Int32的))
                MyDataTable.Columns.Add(列1,Type.GetType(System.Int32的))
                MyDataTable.Columns.Add(列2,Type.GetType(System.Int32的))
                MyDataTable.Columns.Add(Column3,Type.GetType(System.Int32的))
                MyDataTable.Columns.Add(Column4,Type.GetType(System.Int32的))
            结束使用
        抓住EX为例外
            .WriteError(子DefineDataTable,ex.Message)
        结束尝试
    结束与
结束小组
 

但是的SqlDataAdapter 未更新:

 公共共享子UpdateTable()
    昏暗ErrorEmail作为新ErrorEmailMessageClass

    随着ErrorEmail
        尝试
            使用连接作为新的SqlConnection(My.Settings.MyDB)
                connection.Open()

                昏暗的适配器作为新的SqlDataAdapter()

                设置更新命令和参数。
                adapter.UpdateCommand =新的SqlCommand(_
                  UPDATE Schema.TableTwo_
                  &放大器; 组  _
                  &放大器; 列1 = @列1,_
                  &放大器; 列2 = @列2,_
                  &放大器; Column3 = @ Column3,_
                  &放大器; Column4 = @ Column4_
                  &放大器; WHE​​RE TableOneId = @ ID;连接)
                adapter.UpdateCommand.Parameters.Add(@列1,SqlDbType.Int,4,列1)
                adapter.UpdateCommand.Parameters.Add(@列2,SqlDbType.Int,4,列2)
                adapter.UpdateCommand.Parameters.Add(@ Column3,SqlDbType.Int,4,Column3)
                adapter.UpdateCommand.Parameters.Add(@ Column4,SqlDbType.Int,4,Column4)
                adapter.UpdateCommand.Parameters.Add(@ IDSqlDbType.Int,4,ID)
                adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.OutputParameters

                设置批量大小。
                adapter.UpdateBatchSize = 0

                执行更新。
                adapter.Update(MyDataTable)

                的Connection.close()
            结束使用
        抓住EX为例外
            .WriteError(子UpdateTable,ex.Message)
        结束尝试
    结束与
结束小组
 

解决方案

正如您所评价的<一个href="http://stackoverflow.com/questions/7152273/batch-update-table-across-non-linked-sql-servers/7153047#7153047">your另一个问题,这里就是我的回答:

您应该设置<一个href="http://msdn.microsoft.com/en-us/library/system.data.common.dbdataadapter.updatebatchsize%28v=VS.100%29.aspx"相对=nofollow>中的SqlDataAdapter为0(无限制)UpdateBatchSize 的财产。 我不明白的方式,而不循环表1至表2更新

下面是一个简单的code向您展示了实现这一目标的一种方法:

 公用Sub BATCHUPDATE(BYVAL表1为数据表)
    昏暗connectionStringServer2作为字符串= GetConnectionString()

    使用连接作为新的SqlConnection(connectionStringServer2)
        昏暗的适配器作为新的SqlDataAdapter()

        设置更新命令和参数
        adapter.UpdateCommand =新的SqlCommand(_
          UPDATE表2 SET_
          &放大器; NAME = @名称,日期= @日期WHERE TableOneId = @ TableOneId;_
          连接)
        adapter.UpdateCommand.Parameters.Add(@名称,_
          SqlDbType.NVarChar,50,姓名)
        adapter.UpdateCommand.Parameters.Add(@日,_
          SqlDbType.DateTime,0,日期)
        adapter.UpdateCommand.Parameters.Add(@ TableOneId,_
        SqlDbType.Int,0,TableOneId)
        adapter.UpdateCommand.UpdatedRowSource = _
          UpdateRowSource.None

        '设置批量大小,'
        尝试更新所有行的一个往返到服务器
        adapter.UpdateBatchSize = 0
        你可能想增加更新命令的的CommandTimeout以及'
        adapter.UpdateCommand.CommandTimeout = 600 '10分钟'

        昏暗表2作为新的DataTable(表2)
        table2.Columns.Add(新的DataColumn(名称,的GetType(字符串)))
        table2.Columns.Add(新的DataColumn(日期,的GetType(日期)))
        table2.Columns.Add(新的DataColumn(TableOneId的GetType(的Int32)))

        复制内容从表1至表2
        对于每一行作为的DataRow在table1.Rows
            昏暗NEWROW = table2.NewRow
            NEWROW(TableOneId)=行(ID)
            NEWROW(名称)=行(名称)
            NEWROW(日期)=排(日期)
            table2.Rows.Add(NEWROW)
            注:我没有测试以下,但它可能工作,或者给你一个线索
            newRow.AcceptChanges()
            newRow.SetModified()
        下一个

        执行更新
        AddHandler的adapter.RowUpdated,_
        新SqlRowUpdatedEventHandler(AddressOf OnRowUpdated)

        adapter.UpdateBatchSize = 5000
        adapter.UpdateCommand.CommandTimeout = 6000
        adapter.ContinueUpdateOnError = TRUE
        adapter.Update(表2)

    结束使用
结束小组
私人共享子OnRowUpdated(发件人为对象,ARGS作为SqlRowUpdatedEventArgs)
    如果args.RecordsAffected = 0然后
        args.Row.RowError =并发冲突!
        args.Status = UpdateStatus.SkipCurrentRow
    结束如果
结束小组
 

I am trying to update TableTwo using a DataTable built using TableOne. The relationship between tables is a foreign column called TableOneId inside TableTwo.

I used the following code sample to make this work: Performing Batch Operations Using DataAdapters (MSDN)

The DataTable is populated in another Public Shared function.

I can't figure out what is wrong. No error messages are reported. The watch reveals that the DataTable is loaded with data.

The DataTable is defined as:

Public MyDataTable As New DataTable

Public Shared Sub DefineDataTable()

    Dim ErrorEmail As New ErrorEmailMessageClass
    With ErrorEmail
        Try
            Using connection As New SqlConnection(My.Settings.MyDB)
                MyDataTable.Columns.Add("ID", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column1", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column2", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column3", Type.GetType("System.Int32"))
                MyDataTable.Columns.Add("Column4", Type.GetType("System.Int32"))
            End Using
        Catch ex As Exception
            .WriteError("Sub DefineDataTable", ex.Message)
        End Try
    End With
End Sub

But the SqlDataAdapter is not updating:

Public Shared Sub UpdateTable()
    Dim ErrorEmail As New ErrorEmailMessageClass

    With ErrorEmail
        Try
            Using connection As New SqlConnection(My.Settings.MyDB)
                connection.Open()

                Dim adapter As New SqlDataAdapter()

                'Set the UPDATE command and parameters.
                adapter.UpdateCommand = New SqlCommand( _
                  "UPDATE Schema.TableTwo " _
                  & "SET " _
                  & "Column1=@Column1, " _
                  & "Column2=@Column2, " _
                  & "Column3=@Column3, " _
                  & "Column4=@Column4 " _
                  & "WHERE TableOneId=@ID;", connection)
                adapter.UpdateCommand.Parameters.Add("@Column1", SqlDbType.Int, 4, "Column1")
                adapter.UpdateCommand.Parameters.Add("@Column2", SqlDbType.Int, 4, "Column2")
                adapter.UpdateCommand.Parameters.Add("@Column3", SqlDbType.Int, 4, "Column3")
                adapter.UpdateCommand.Parameters.Add("@Column4", SqlDbType.Int, 4, "Column4")
                adapter.UpdateCommand.Parameters.Add("@ID", SqlDbType.Int, 4, "ID")
                adapter.UpdateCommand.UpdatedRowSource = UpdateRowSource.OutputParameters

                ' Set the batch size.
                adapter.UpdateBatchSize = 0

                ' Execute the update.
                adapter.Update(MyDataTable)

                connection.Close()
            End Using
        Catch ex As Exception
            .WriteError("Sub UpdateTable", ex.Message)
        End Try
    End With
End Sub

解决方案

As you've commented in your other question, here is my answer:

You should set the UpdateBatchSize property of the SqlDataAdapter to 0 (unlimited). I don't see a way to update table2 without looping table1.

Here is a sample code to show you one way to achieve this:

Public Sub BatchUpdate(ByVal table1 As DataTable)
    Dim connectionStringServer2 As String = GetConnectionString()

    Using connection As New SqlConnection(connectionStringServer2)
        Dim adapter As New SqlDataAdapter()

        'Set the UPDATE command and parameters'
        adapter.UpdateCommand = New SqlCommand( _
          "UPDATE Table2 SET " _
          & "NAME=@NAME,Date=@Date  WHERE TableOneId=@TableOneId;", _
          connection)
        adapter.UpdateCommand.Parameters.Add("@Name", _
          SqlDbType.NVarChar, 50, "Name")
        adapter.UpdateCommand.Parameters.Add("@Date", _
          SqlDbType.DateTime, 0, "Date")
        adapter.UpdateCommand.Parameters.Add("@TableOneId", _
        SqlDbType.Int, 0, "TableOneId")
        adapter.UpdateCommand.UpdatedRowSource = _
          UpdateRowSource.None

        ' Set the batch size,' 
        ' try to update all rows in a single round-trip to the server'
        adapter.UpdateBatchSize = 0
        ' You might want to increase the UpdateCommand's CommandTimeout as well'
        adapter.UpdateCommand.CommandTimeout = 600 '10 minutes'

        Dim table2 As New DataTable("table2")
        table2.Columns.Add(New DataColumn("Name", GetType(String)))
        table2.Columns.Add(New DataColumn("Date", GetType(Date)))
        table2.Columns.Add(New DataColumn("TableOneId", GetType(Int32)))

        ' copy content from table1 to table2'
        For Each row As DataRow In table1.Rows
            Dim newRow = table2.NewRow
            newRow("TableOneId") = row("ID")
            newRow("Name") = row("Name")
            newRow("Date") = row("Date")
            table2.Rows.Add(newRow)    
            ' note: i have not tested following, but it might work or give you a clue'
            newRow.AcceptChanges()
            newRow.SetModified()
        Next

        ' Execute the update'
        AddHandler adapter.RowUpdated, _
        New SqlRowUpdatedEventHandler(AddressOf OnRowUpdated)

        adapter.UpdateBatchSize = 5000   
        adapter.UpdateCommand.CommandTimeout = 6000
        adapter.ContinueUpdateOnError = True                      
        adapter.Update(table2)

    End Using
End Sub
Private Shared Sub OnRowUpdated(sender As Object, args As SqlRowUpdatedEventArgs)
    If args.RecordsAffected = 0 Then
        args.Row.RowError = "Optimistic Concurrency Violation!"
        args.Status = UpdateStatus.SkipCurrentRow
    End If
End Sub

这篇关于SqlDataAdapter的不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆