Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Update SQL Server Table with a Spreadsheet Worksheet DataTable using CommandBuilder

Update SQL Server Table with a Spreadsheet Worksheet DataTable using CommandBuilder

Scheduled Pinned Locked Moved Database
databasesql-serversysadminannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LHendren
    wrote on last edited by
    #1

    I have created a DataTable from a spreadsheet worksheet and want to UPDATE my SQL table, but am struggling. I cannot update the single table having a Primary Key with the changed column information. Should be easy and straight forward. The ShowResult(dataTable) depicts the correct DataTable information. (Using some DevExpress expressions) ' Create a data table with column names obtained from the first row in a range if it has headers. ' Column data types are obtained from cell value types of cells in the first data row of the worksheet range. Dim dtpHExportDataTable As DataTable = worksheet.CreateDataTable(range, rangeHasHeaders) ' 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() 'A custom method that displays the resulting data table. ShowResult(dtpHExportDataTable) 'Looks good! 'Now, my code to Update the "Result" column in my sql table with the datatable information Dim cnSQL As SqlConnection = dbLIMS.GetLIMSConnection Using adapter = New SqlDataAdapter("SELECT * FROM Analytical_Sample_Log_ResultsInfo", cnSQL) Using New SqlCommandBuilder(adapter) adapter.Fill(dtpHExportDataTable) cnSQL.Open() adapter.Update(dtpHExportDataTable) End Using End Using

    M 1 Reply Last reply
    0
    • L LHendren

      I have created a DataTable from a spreadsheet worksheet and want to UPDATE my SQL table, but am struggling. I cannot update the single table having a Primary Key with the changed column information. Should be easy and straight forward. The ShowResult(dataTable) depicts the correct DataTable information. (Using some DevExpress expressions) ' Create a data table with column names obtained from the first row in a range if it has headers. ' Column data types are obtained from cell value types of cells in the first data row of the worksheet range. Dim dtpHExportDataTable As DataTable = worksheet.CreateDataTable(range, rangeHasHeaders) ' 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() 'A custom method that displays the resulting data table. ShowResult(dtpHExportDataTable) 'Looks good! 'Now, my code to Update the "Result" column in my sql table with the datatable information Dim cnSQL As SqlConnection = dbLIMS.GetLIMSConnection Using adapter = New SqlDataAdapter("SELECT * FROM Analytical_Sample_Log_ResultsInfo", cnSQL) Using New SqlCommandBuilder(adapter) adapter.Fill(dtpHExportDataTable) cnSQL.Open() adapter.Update(dtpHExportDataTable) End Using End Using

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You can get an empty datatable from SQL with Select * from Tablename where 1-1. This will give you the SQL data type, they may differ from the Excel data type. An update statement will look like

      Update TableName set field1 = ValueFromExcelCell, field2 = ... where PrimareyKeyField = PrimaryKeyValueFromExcel

      The update needs to be execute for each row in the ExcelTable where the data has changed

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups