0

SQLCommandBuilder次のテストコードを使用して、データテーブル行を使用してデータベーステーブル行を更新しようとしています。シンプルに保つために、主キー列と 1 つのデータテーブルを持つ 1 つのテーブル。

次のコードを使用すると、dbo.Dogs2テーブルにデータテーブル行が「追加」されるため、変更された行を更新するだけでなく、行数が 2 倍になります。

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
4

1 に答える 1

0

アダプタの使い方が間違っています。最初にデータベースから行をロードし、次に取得した行を更新して、最後に update を呼び出す必要があります。

 ' 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

既存のテーブルをアダプタの Fill メソッドに渡すと、既存のレコードは削除されないため、テーブルはデータベースからのデータとテーブルの手動作成からのデータで満たされます (もちろん、アダプタはテーブルの列を作成します)さらに、テーブルに手動で追加された行は でマークDataRowState.Addedされ、コードによって変更された行は でマークされますDataRowState.Changed。この状態は、更新コマンドがテーブルに存在するすべての行に対して実行するアクションを決定するのに役立ちます (もちろん、変更されていない行は初期状態を維持しますDataRowState.Unchanged

最後に、AcceptChanges を呼び出しても、データベース テーブルで行が更新されるわけではありません。DataRowState フラグのみがリセットされます。DataRowState.Unchanged

于 2015-08-28T19:37:05.337 に答える