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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. which is better when inserting data in sql database

which is better when inserting data in sql database

Scheduled Pinned Locked Moved Database
databasehelpquestion
4 Posts 4 Posters 1 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.
  • N Offline
    N Offline
    neodeaths
    wrote on last edited by
    #1

    hi i was just wondering which is the better way to insert data into database when i got large amount of data to add in. way 1 do "insert into db1 () values()" execute command while() or way 2 do string = string + "insert into db1 () values()" while() execute command also if lets say way 2 somewhere in the middle of the insert command there is an error would the code before it still be added?

    P I N 3 Replies Last reply
    0
    • N neodeaths

      hi i was just wondering which is the better way to insert data into database when i got large amount of data to add in. way 1 do "insert into db1 () values()" execute command while() or way 2 do string = string + "insert into db1 () values()" while() execute command also if lets say way 2 somewhere in the middle of the insert command there is an error would the code before it still be added?

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

      The first is better, but neither is as good as:

      cmd.CommandText = "INSERT INTO table ( ... ) VALUES ( @Param0 ... )" ;

      (create and add parameters to cmd)

      while ( ... )
      {
      (set parameter values)

      cmd.ExecuteNonQuery() ;
      

      }

      And use a transaction if you need to.

      1 Reply Last reply
      0
      • N neodeaths

        hi i was just wondering which is the better way to insert data into database when i got large amount of data to add in. way 1 do "insert into db1 () values()" execute command while() or way 2 do string = string + "insert into db1 () values()" while() execute command also if lets say way 2 somewhere in the middle of the insert command there is an error would the code before it still be added?

        I Offline
        I Offline
        i i i
        wrote on last edited by
        #3

        "in the middle of the insert command there is an error would the code before it still be added?" You can use sqlTransaction , and can rollback and commit transactions as well as can make commit points

        Best Of Regards, SOFTDEV If you have knowledge, let others light their candles at it

        1 Reply Last reply
        0
        • N neodeaths

          hi i was just wondering which is the better way to insert data into database when i got large amount of data to add in. way 1 do "insert into db1 () values()" execute command while() or way 2 do string = string + "insert into db1 () values()" while() execute command also if lets say way 2 somewhere in the middle of the insert command there is an error would the code before it still be added?

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          Best way to see which is performing fast is to profile the application. I will go with the first method with the modifications what PIEBALDconsult suggested. Second method would be good in performance but it is hard to troubleshoot when you get error in one of the inserts.

          neodeaths wrote:

          also if lets say way 2 somewhere in the middle of the insert command there is an error would the code before it still be added? Quote Selected Text

          It will be added. If you want to ensure all the data gets added, run the queries in a transaction.

          Navaneeth How to use google | Ask smart questions

          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