简体   繁体   中英

Update SQL Database Table Using a TableAdapter with SqlCommandBuilder and a Worksheet exported to a DataTable

I am exporting data from a spreadsheet worksheet range into a datatable (dtpHExportDataTable). When I call ShowResult(dtpHExportDataTable), the grid form displays the correct datatable information (headers and rows of data). Likewise, when I call ShowResult(resultsDataTable) the grid form displays the correct datatable information (the database table rows including the updated rows)

    Using cnSQL1 As New SqlConnection
        cnSQL1.ConnectionString = cnString
        Using adapter1 = New SqlDataAdapter("SELECT SampleNo, Results, Complete_Date, Dex_Row_Id " _
                                            & "FROM LIMS.dbo.Analytical_Sample_Log_ResultsInfo", cnSQL1)
            Dim builder1 As New SqlCommandBuilder(adapter1)

            adapter1.UpdateCommand = builder1.GetUpdateCommand()

            Using New SqlCommandBuilder(adapter1)

                adapter1.Fill(resultsDataTable)

                resultsDataTable.PrimaryKey = New DataColumn() {resultsDataTable.Columns("Dex_Row_Id")}

                dtpHExportDataTable = resultsDataTable.Clone()

                     ‘not sure if needed, just trying to make sure datatypes are correct
                dtpHExportDataTable.Columns("SampleNo").DataType = System.Type.GetType("System.Int32")
                dtpHExportDataTable.Columns("Results").DataType = System.Type.GetType("System.String")
                dtpHExportDataTable.Columns("Complete_Date").DataType = System.Type.GetType("System.DateTime")
                dtpHExportDataTable.Columns("Dex_Row_Id").DataType = System.Type.GetType("System.Int32")

                ' Create the exporter that obtains data from the specified range, 
                ' skips header row if required and populates the specified data table. 
                Dim exporter As DataTableExporter = workSheet.CreateDataTableExporter(range, dtpHExportDataTable, rangeHasHeaders)

                AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError
                ' Specify exporter options.
                exporter.Options.ConvertEmptyCells = True
                exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0
                ' Perform the export.
                exporter.Export()

   ShowResult(dtpHExportDataTable)  ‘grid form shows expected information

                For index = 1 To dtpHExportDataTable.Rows.Count - 1
                    dtpHExportDataTable.Rows(index).SetModified()
                Next

                resultsDataTable.Merge(dtpHExportDataTable)

                ShowResult(resultsDataTable)    ‘grid form shows expected information

                Try
                    adapter1.Update(resultsDataTable)
                Catch ex As Exception
                    MsgBox("Update failed")
                End Try
            End Using
        End Using
    End Using

My tableadapter Query Builder, Update command text is “UPDATE Analytical_Sample_Log_ResultsInfo SET SampleNo = @SampleNo, Results = @Results, Complete_Date = @Complete_Date WHERE (Dex_Row_Id = @Original_Dex_Row_Id)

In essence, the datatables populate correctly; however, the tableadapter is not updating the sql database table even though an exception is not thrown.

This is my final code, and it works for me. Note the change in which datatable is being modified, the revised location of SetModified, and the addition of the updateStatement since I am updating only 4 of the 11 columns in the database table.

    Using adapter1 = New SqlDataAdapter("SELECT SampleNo, Results, Complete_Date, Dex_Row_Id " _
                                          & "FROM LIMS.dbo.Analytical_Sample_Log_ResultsInfo", cnSQL1)

            Dim builder1 As New SqlCommandBuilder(adapter1)
            adapter1.UpdateCommand = builder1.GetUpdateCommand()
            Using New SqlCommandBuilder(adapter1)
                adapter1.Fill(resultsDataTable)

                resultsDataTable.PrimaryKey = New DataColumn() {resultsDataTable.Columns("Dex_Row_Id")}

                dtpHExportDataTable = resultsDataTable.Clone()

                Dim exporter As DataTableExporter = workSheet.CreateDataTableExporter(range, dtpHExportDataTable, rangeHasHeaders)

                AddHandler exporter.CellValueConversionError, AddressOf exporter_CellValueConversionError

                exporter.Options.ConvertEmptyCells = True
                exporter.Options.DefaultCellValueToColumnTypeConverter.EmptyCellValue = 0

                exporter.Export()

                For index = 1 To resultsDataTable.Rows.Count - 1
                    resultsDataTable.Rows(index).SetModified()
                Next

                resultsDataTable.Merge(dtpHExportDataTable)


                Dim updateStatement As String = "UPDATE LIMS.dbo.Analytical_Sample_Log_ResultsInfo SET [SampleNo] = @SampleNo, [Results] = @Results, [Complete_Date] = @Complete_Date " _
                                              & "WHERE [Dex_Row_Id] = @Dex_Row_Id"

                Dim updateCommand As New SqlCommand(updateStatement, cnSQL1)

                updateCommand.Parameters.Add("@SampleNo", SqlDbType.Int, 0, "SampleNo")
                updateCommand.Parameters.Add("@Results", SqlDbType.NChar, 10, "Results")
                updateCommand.Parameters.Add("@Complete_Date", SqlDbType.Date, 10, "Complete_Date")
                updateCommand.Parameters.Add("@Dex_Row_Id", SqlDbType.Int, 0, "Dex_Row_Id")

                adapter1.UpdateCommand = updateCommand

                adapter1.Update(resultsDataTable)


            End Using

        End Using

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM