简体   繁体   中英

How to Insert data from datagridview Rows to mysql database in vb.net using stored procedure

Im stucked. I am trying to insert data from the grid to the database.But I'm stucked. 在此处输入图片说明

Slugsie's comment is pertinent, but needs to go a little further:

You're actually clearing the entire parameters collection before you call the procedure. If it were a house, you went to all the effort to build the whole thing, then at the last minute you demolish it and show the customer a cleared lot

Don't clear the Parameters collection at all. Add all the parameters to it OUTSIDE the loop, then inside the loop, set the values:


    'general pattern
    cmd.Parameters.AddWithValue("param1", "...")
    cmd.Parameters.AddWithValue("param2", "...")
    cmd.Parameters.AddWithValue("param3", "...")
    cmd.Parameters.AddWithValue("param4", "...")

    For Each something In somethingElse
      cmd.Parameters("param1").Value = something.Whatever1
      cmd.Parameters("param2").Value = something.Whatever2
      cmd.Parameters("param3").Value = something.Whatever3
      cmd.Parameters("param4").Value = something.Whatever4
      cmd.Execute...
    Next something

That's the pattern you should be adopting


Though jmc also makes a useful comment; you could make your life a lot easier by

  • adding a DataSet type file to your project
  • open it, right click the surface, choose Add TableAdapter
  • when configuring the wizard for the adapter, say you want to use stored procedures for your select, insert, update etc
  • wire the procedures up to the relevant table columns

Now in code you can save data just by a single line or two:

Dim t as New WhateverTableAdapter
t.Update(yourWhateverDatatable) 'the WhateverDataTable is bound to a grid, the user has edited the rows in it by changing the grid

That Update() call will perform any necessary use of the Insert/Update/Delete procedures depending on if the datarow in the table is an Added, Modified or Deleted state

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM