Results 1 to 9 of 9

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

Threaded View

  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

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