Results 1 to 9 of 9

Thread: [2008] Inserting/updating multiple rows into a database's table

  1. #1

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    [2008] Inserting/updating multiple rows into a database's table

    I was curious, how do other people insert/update multiple rows into a database's table?

    If I got a loop and I'm enumerating some list that I need to push multiple Insert/Update commands to a database, in the past I've directly embedded values into SQL and run them as a single command:

    Code:
            Dim strSQL As String = String.Empty
    
            For Each s As String In strList
                strSQL &= String.Format("INSERT INTO MyTable (ID, Name) VALUES ('{0}', '{1}'); ", strID, s)
            Next
            Dim cmd As New MySql.Data.MySqlClient.MySqlCommand(strSQL, con)
            cmd.ExecuteNonQuery()
    Though it's easy, I don't like doing this for obvious reasons (single quotes come to mind) and would rather use parameters, but then I'd have to make a parameter for every line.

    I could use multiple Command objects, but this would be extremely slow if I have a substantial amount of data to insert and I'd be inserting them one-by-one. I suppose I could use a DataAdapter, but building and pushing updates to DataAdapters always seems like a major pain in the rear (especially if I have a lot of fields)

    How do you guys do it in this situation? Is there some trick I'm missing for pushing multiple inserts/updates? Should I just make a DataTable + DataAdapter and push multiple updates/inserts that way?
    Last edited by Jenner; Aug 19th, 2008 at 08:46 AM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Inserting multiple rows into a DataTable

    Here's how I do it:
    1) Start the loop
    2) Build the command object
    3) Add the parameters
    4) Execute the command
    5) Repeat loop until done.

    plain and simple. Just the way I like it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] Inserting multiple rows into a DataTable

    Yea, but when I do it that way, performance stinks. What would take a few seconds can take more than a minute when I have to populate a large table.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Inserting multiple rows into a DataTable

    You've knind of contradicted yourself there. First you ask:
    I was curious, how do other people insert/update multiple rows into a DataTable?
    and then you ask about saving data to a database using methods that wouldn't involve a DataTable. Why would you use a DataTable at all if you're not going to use a DataAdapter or TableAdapter to save the data with an Update call? That's the way I'd save multiple records.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] Inserting multiple rows into a DataTable

    Sorry, I meant insert/update multiple rows into a database's table.

    (updated first post and title)
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] Inserting/updating multiple rows into a database's table

    I do it this way..
    1>Update/Delete/Insert new rows in Datatable this way each row has it's own rowstate value
    2>Create a Insert/Update/Delete SQLCommand Object with Parameter assign it to DataAdapter.
    3>Call DataAdapter's Update method,this will update the Database table based i.e it will insert/delete/update rows based on each rows rowstate value.
    __________________
    Rate the posts that helped you

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Inserting/updating multiple rows into a database's table

    Quote Originally Posted by riteshjain1982
    I do it this way..
    1>Update/Delete/Insert new rows in Datatable this way each row has it's own rowstate value
    2>Create a Insert/Update/Delete SQLCommand Object with Parameter assign it to DataAdapter.
    3>Call DataAdapter's Update method,this will update the Database table based i.e it will insert/delete/update rows based on each rows rowstate value.
    Further to this, you can set the UpdateBatchSize of the DataAdapter to make the operation more efficient by sending multiple records to the database at a time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] Inserting/updating multiple rows into a database's table

    What does UpdateBatchSize default to? I assume one command?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2008] Inserting/updating multiple rows into a database's table

    Quote Originally Posted by Jenner
    What does UpdateBatchSize default to? I assume one command?
    Why assume, or ask, when you can test? Create an adapter and test the value. That's about 20 seconds work and you have your answer.
    Last edited by jmcilhinney; Aug 19th, 2008 at 06:51 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width