[英]How to Insert data from datagridview Rows to mysql database in vb.net using stored procedure
Slugsie's comment is pertinent, but needs to go a little further: Slugsie 的评论是中肯的,但需要更进一步:
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.根本不要清除 Parameters 集合。 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;尽管 jmc 也发表了有用的评论; you could make your life a lot easier by
你可以让你的生活更轻松
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该 Update() 调用将执行插入/更新/删除过程的任何必要使用,具体取决于表中的数据行是已添加、已修改还是已删除状态
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.