SQLCommandBuilder - 仅来自DataTable的更新"追加"数据库表

时间:2015-08-28 19:23:50

标签: sql-server vb.net datatable sqlcommandbuilder

我正在尝试使用带有数据表行的SQLCommandBuilder使用以下测试代码来更新数据库表行。一个表具有主键列和一个数据表以保持简单。

使用以下代码,dbo.Dogs2表格为"附加"使用数据表行 - 因此将行数加倍而不仅仅是更新已更改的行

如果我在table.AcceptChanges()之前添加代码Dim builder As New SqlCommandBuilder(adapter),则数据库表dbo.Dogs2保持不变。

如果我在table.AcceptChanges()之前添加代码adapter.Update(table),则数据库表dbo.Dogs2保持不变。

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        ' dbo.Dogs2 database table columns are exactly like datatable columns with exception of dog names
         ' only UPDATING the "Name" field (no Inserts or deletes)
         ' orginal dog names "Name" in Dogs2.dbo are Sharpy, Bully, Shep, Charlie, and Yorky
         ' new dog names "Name" in Dogs2.dbo are June, Tucker, Maggie, Charles, and Candy
         ' Dex_Row_Id is the primary key with Identity Increment set to 1

        ' Create a DataTable with five columns.
         '
         Dim table As New DataTable()
         table.Columns.Add("Weight", GetType(Integer))
         table.Columns.Add("Name", GetType(String))
         table.Columns.Add("Breed", GetType(String))
         table.Columns.Add("Size", GetType(Char))
         table.Columns.Add("Date", GetType(DateTime))
         table.Columns.Add("Dex_Row_Id", GetType(Integer))
         '
         ' Add data to the DataTable
         '
         AddDogRow(table, 57, "June", "Shar Pei")
         AddDogRow(table, 130, "Tucker", "Bullmastiff")
         AddDogRow(table, 92, "Maggie", "Anatolian Shepherd Dog")
         AddDogRow(table, 25, "Charles", "Cavalier King Charles Spaniel")
         AddDogRow(table, 7, "Candy", "Yorkshire Terrier")

        ShowResult(table)    'displays datatable correctly    (this is a DevExpress.com Reference/Extension)
         '
         ' Create new SqlConnection, SqlDataAdapter, and builder.
         '
         Dim cnString As String = "<<<SQLConnectionString>>>"
         '
         Using cnSQL1 As New SqlConnection
             cnSQL1.ConnectionString = cnString

            Using adapter = New SqlDataAdapter("SELECT * FROM Dogs2", cnSQL1)

                ShowResult(table)  'displays datatable

                Dim builder As New SqlCommandBuilder(adapter)
                 adapter.UpdateCommand = builder.GetUpdateCommand()
                 builder.RefreshSchema()

                Using New SqlCommandBuilder(adapter)
                     '
                     ' Fill the DataAdapter with the values in the DataTable.
                     '
                     adapter.Fill(table)  

                    ShowResult(table)  'displays datatable + original table data

                    ' Open the connection to the SQL database.
                     '
                     cnSQL1.Open()

                    ' Update the SQL database table with the values.
                     '
                     adapter.Update(table)

                    ' dbo.Dogs2 now has 10 rows  (the 5 rows from the dataset + the original 5 rows)

                End Using

            End Using

        End Using

    End Sub

1 个答案:

答案 0 :(得分:0)

您使用的适配器不正确。您应该首先从数据库加载行,然后更新检索到的行,最后调用更新。

string

当您将现有表传递给适配器的Fill方法时,不会删除现有记录,因此您的表中将填充数据库中的数据以及手动创建表(当然适合您的适配器构建)表格列。此外,手动添加到您的表中的行标有DataRowState.Added,而您的代码修改的行将标记为 ' REMOVE THE CODE BEFORE THIS ' ' Create new SqlConnection, SqlDataAdapter, and builder.' Dim cnString As String = "<<<SQLConnectionString>>>" Dim table = New DataTable() ' Leave it emtpy and without schema' Using cnSQL1 As New SqlConnection cnSQL1.ConnectionString = cnString Using adapter = New SqlDataAdapter("SELECT * FROM Dogs2", cnSQL1) Dim builder As New SqlCommandBuilder(adapter) adapter.UpdateCommand = builder.GetUpdateCommand() ' no need of this -> builder.RefreshSchema()' Using New SqlCommandBuilder(adapter) adapter.Fill(table) ShowResult(table) 'displays original table data' ' no need of this -> cnSQL1.Open()' ' NOW YOU COULD CHANGE THE ROWS, FOR EXAMPLE' table.Rows(0)("Weight") = 99 ' Update the SQL database table with the values.' adapter.Update(table) End Using End Using End Using 。此状态有助于Update命令决定哪个操作对表中存在的每一行执行(当然,未更改的行保持初始DataRowState.Changed

最后,calling AcceptChanges并不意味着将在数据库表上更新行。只有DataRowState标志被重置为DataRowState.Unchanged