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. General Programming
  3. C#
  4. Batch update in SQL Server Database

Batch update in SQL Server Database

Scheduled Pinned Locked Moved C#
databasesql-serversysadminannouncement
6 Posts 3 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.
  • S Offline
    S Offline
    sirtimid
    wrote on last edited by
    #1

    Hello all, I have a windows application which updates multiple rows (using a stored procedure in the db) in a SQL Server database, but this updates run many times sequentially in a for loop. For the update methos I have tried the following methods: - The SqlCommand object running my stored procedure as a text commnand and executing it with the ExecuteNonQuery method. - An SqlDataAdapter using its UpdateCommand attribute and executing using the ExecuteNonQuery method. In both ways the running time is really slow. Can anyone suggest me an other way to do the updates. It would be good if I could save my updates n a dataset or a datatable and after save it to the db. Thanks in Advance Dimitris

    R P 2 Replies Last reply
    0
    • S sirtimid

      Hello all, I have a windows application which updates multiple rows (using a stored procedure in the db) in a SQL Server database, but this updates run many times sequentially in a for loop. For the update methos I have tried the following methods: - The SqlCommand object running my stored procedure as a text commnand and executing it with the ExecuteNonQuery method. - An SqlDataAdapter using its UpdateCommand attribute and executing using the ExecuteNonQuery method. In both ways the running time is really slow. Can anyone suggest me an other way to do the updates. It would be good if I could save my updates n a dataset or a datatable and after save it to the db. Thanks in Advance Dimitris

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      Are you just updating rows, or are you inserting new ones and deleting them too? One approach you might consider is rather than repeatedly calling an update proc, instead insert each modification into a temporary table and then call a proc which would do the update in one single atomic shot. The advantage of this would be that you could bulk load that temporary table really quickly. If that's too much, have you 'prepared' the proc so that you just change the value of the SqlParameters rather than create new ones each time?

      Regards, Rob Philpott.

      S 1 Reply Last reply
      0
      • R Rob Philpott

        Are you just updating rows, or are you inserting new ones and deleting them too? One approach you might consider is rather than repeatedly calling an update proc, instead insert each modification into a temporary table and then call a proc which would do the update in one single atomic shot. The advantage of this would be that you could bulk load that temporary table really quickly. If that's too much, have you 'prepared' the proc so that you just change the value of the SqlParameters rather than create new ones each time?

        Regards, Rob Philpott.

        S Offline
        S Offline
        sirtimid
        wrote on last edited by
        #3

        Thank you Rob, Actually my stored procedure has a double functionality. Updates rows in my table (depending on a key) and in case of non existing rows it inserts them. I haven't tried to bulk insert in a temp table and after update mine because I have to do this many times and I'm afraid that it would be realy time consuming to do this every time, because every time I should delete the temp table. That's why I would like to fill somehow a datatable in the memory and after my insert/update queries (maybe without using stored procedure) to update this in the db. Thanks again, Dimitris

        R 1 Reply Last reply
        0
        • S sirtimid

          Hello all, I have a windows application which updates multiple rows (using a stored procedure in the db) in a SQL Server database, but this updates run many times sequentially in a for loop. For the update methos I have tried the following methods: - The SqlCommand object running my stored procedure as a text commnand and executing it with the ExecuteNonQuery method. - An SqlDataAdapter using its UpdateCommand attribute and executing using the ExecuteNonQuery method. In both ways the running time is really slow. Can anyone suggest me an other way to do the updates. It would be good if I could save my updates n a dataset or a datatable and after save it to the db. Thanks in Advance Dimitris

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #4

          I use a parameterized statement with ExecuteNonQuery, usually with a transaction.

          1 Reply Last reply
          0
          • S sirtimid

            Thank you Rob, Actually my stored procedure has a double functionality. Updates rows in my table (depending on a key) and in case of non existing rows it inserts them. I haven't tried to bulk insert in a temp table and after update mine because I have to do this many times and I'm afraid that it would be realy time consuming to do this every time, because every time I should delete the temp table. That's why I would like to fill somehow a datatable in the memory and after my insert/update queries (maybe without using stored procedure) to update this in the db. Thanks again, Dimitris

            R Offline
            R Offline
            Rob Philpott
            wrote on last edited by
            #5

            I wouldn't be too concerned about the overhead of creating a table each time, this would be far more effecient than multiple calls to an update proc. AFAIK, there isn't really a way to bulk update a database, but you can bulk insert into it very quickly. So, if you did want to try this approach, create a new prepare proc which creates the table, use the SqlBulkCopy class to rapidly write to this, and then call a commit proc which updates and inserts based on this table and deletes it afterwards.

            Regards, Rob Philpott.

            S 1 Reply Last reply
            0
            • R Rob Philpott

              I wouldn't be too concerned about the overhead of creating a table each time, this would be far more effecient than multiple calls to an update proc. AFAIK, there isn't really a way to bulk update a database, but you can bulk insert into it very quickly. So, if you did want to try this approach, create a new prepare proc which creates the table, use the SqlBulkCopy class to rapidly write to this, and then call a commit proc which updates and inserts based on this table and deletes it afterwards.

              Regards, Rob Philpott.

              S Offline
              S Offline
              sirtimid
              wrote on last edited by
              #6

              Yes maybe you are right. I have tried I think everything else. So I'll try this solution. Thank you very much for your help Rob :)

              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