我正在将电子表格工作表范围中的数据导出到数据表 (dtpHExportDataTable) 中。当我调用 ShowResult(dtpHExportDataTable) 时,网格表单会显示正确的数据表信息(标题和数据行)。同样,当我调用 ShowResult(resultsDataTable) 时,网格表单会显示正确的数据表信息(数据库表行包括更新的行)
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
我的 tableadapter 查询生成器,更新命令文本是“UPDATE Analytical_Sample_Log_ResultsInfo SET SampleNo = @SampleNo, Results = @Results, Complete_Date = @Complete_Date WHERE (Dex_Row_Id = @Original_Dex_Row_Id)
本质上,数据表正确填充;但是,即使没有抛出异常,tableadapter 也不会更新 sql 数据库表。