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. Insert dataset into database

Insert dataset into database

Scheduled Pinned Locked Moved C#
databasetutorialannouncement
7 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.
  • S Offline
    S Offline
    shoubi
    wrote on last edited by
    #1

    Hi everyone How to insert the records in the dataset into the database. i tried the adapter.update(ds). but it does not work... thank you for reading zheng

    P 1 Reply Last reply
    0
    • S shoubi

      Hi everyone How to insert the records in the dataset into the database. i tried the adapter.update(ds). but it does not work... thank you for reading zheng

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

      DataAdapters, and Update in particular, are very limited in what they can do -- I avoid them. I've forgotten the details, but generally (for SQL Server): 0) Set a Command : INSERT INTO table (field0 ... ) VALUES (@Param0 ... ) 1) Create the parameters : cmd.Parameters.Add ( new SqlParameter ( "@Param0" , typeof(whatever) ) ) 2) Iterate the rows of a DataTable : foreach ( DataRow dr in ds.Table [ i ].Rows ) 2.1) Set the parameter values : cmd.Parameters [ 0 ].Value = dr [ 0 ] ... 2.2) Execute the cmd : cmd.ExecuteNonQuery() 3) Repeat as necessary I prefer to encapsulate this into a method so I don't need to remember it.

      S 1 Reply Last reply
      0
      • P PIEBALDconsult

        DataAdapters, and Update in particular, are very limited in what they can do -- I avoid them. I've forgotten the details, but generally (for SQL Server): 0) Set a Command : INSERT INTO table (field0 ... ) VALUES (@Param0 ... ) 1) Create the parameters : cmd.Parameters.Add ( new SqlParameter ( "@Param0" , typeof(whatever) ) ) 2) Iterate the rows of a DataTable : foreach ( DataRow dr in ds.Table [ i ].Rows ) 2.1) Set the parameter values : cmd.Parameters [ 0 ].Value = dr [ 0 ] ... 2.2) Execute the cmd : cmd.ExecuteNonQuery() 3) Repeat as necessary I prefer to encapsulate this into a method so I don't need to remember it.

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

        hi thank you for reply! below is my code: string SelectSchema = "SELECT * FROM [Package Data Range]"; DataSet ds = new DataSet(); OleDbDataAdapter adapterPkInfo = new OleDbDataAdapter(SelectSchema, ImportCon); OleDbCommandBuilder cmdBldr = new OleDbCommandBuilder(adapterPkInfo); adapterPkInfo.FillSchema(ds, SchemaType.Source, "PackageDataRangeSchema"); DataSet ds2 = new DataSet(); string selectPkInfo = "SELECT * FROM [Package Data Range]"; OleDbDataAdapter adapter = new OleDbDataAdapter(selectPkInfo, connection); adapter.Fill(ds2, "Package Data Range"); ds.Tables[0].Merge(ds2.Tables[0],true,MissingSchemaAction.Ignore); my dataet has around 200 columns. it is not efficient to repeat that.... thank you :) zheng

        P 1 Reply Last reply
        0
        • S shoubi

          hi thank you for reply! below is my code: string SelectSchema = "SELECT * FROM [Package Data Range]"; DataSet ds = new DataSet(); OleDbDataAdapter adapterPkInfo = new OleDbDataAdapter(SelectSchema, ImportCon); OleDbCommandBuilder cmdBldr = new OleDbCommandBuilder(adapterPkInfo); adapterPkInfo.FillSchema(ds, SchemaType.Source, "PackageDataRangeSchema"); DataSet ds2 = new DataSet(); string selectPkInfo = "SELECT * FROM [Package Data Range]"; OleDbDataAdapter adapter = new OleDbDataAdapter(selectPkInfo, connection); adapter.Fill(ds2, "Package Data Range"); ds.Tables[0].Merge(ds2.Tables[0],true,MissingSchemaAction.Ignore); my dataet has around 200 columns. it is not efficient to repeat that.... thank you :) zheng

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

          shoubi wrote:

          it is not efficient to repeat that

          Indeed, but as long as you want to insert all of them, you can loop through the columns and cobble up the Insert statement and parameters.

          S 1 Reply Last reply
          0
          • P PIEBALDconsult

            shoubi wrote:

            it is not efficient to repeat that

            Indeed, but as long as you want to insert all of them, you can loop through the columns and cobble up the Insert statement and parameters.

            S Offline
            S Offline
            shoubi
            wrote on last edited by
            #5

            yup. thank you very much! however just want to check with you the "adapter.update()" how does it work? does it require any commands? or it is smart enough to act accordingly? thank you!:)

            P 1 Reply Last reply
            0
            • S shoubi

              yup. thank you very much! however just want to check with you the "adapter.update()" how does it work? does it require any commands? or it is smart enough to act accordingly? thank you!:)

              P Online
              P Online
              PIEBALDconsult
              wrote on last edited by
              #6

              It requires a SELECT command from which it (actually a CommandBuilder) can produce INSERT, UPDATE, and DELETE commands. Generally, the data must involve one table, have a primary key, and not contain duplicates.

              S 1 Reply Last reply
              0
              • P PIEBALDconsult

                It requires a SELECT command from which it (actually a CommandBuilder) can produce INSERT, UPDATE, and DELETE commands. Generally, the data must involve one table, have a primary key, and not contain duplicates.

                S Offline
                S Offline
                shoubi
                wrote on last edited by
                #7

                ok! thank you :)

                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