简体   繁体   English

如何使用存储过程将数据从datagridview Rows插入到vb.net中的mysql数据库

[英]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: 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你可以让你的生活更轻松

  • adding a DataSet type file to your project将数据集类型文件添加到您的项目
  • open it, right click the surface, choose Add TableAdapter打开它,右键单击表面,选择添加 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该 Update() 调用将执行插入/更新/删除过程的任何必要使用,具体取决于表中的数据行是已添加、已修改还是已删除状态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据从datagridview插入数据库mysql vb.net - how to insert data from datagridview to database mysql vb.net 如何使用vb.net将数据插入Mysql数据库托管 - How to insert data to Mysql database hosting using vb.net VB.NET:将数据从MySQL数据库加载到DataGridView控件中 - VB.NET: Load data from a MySQL database into a DataGridView control 使用 vb.net 中的 datagridview 从数据库显示数据的问题 - Problem in Data display from database using datagridview in vb.net 如何在MySQL数据库中插入图像并使用vb.net检索图像? - How to insert image in MySQL Database and retrive it using vb.net? 多个datagridview数据更新到数据库mysql - vb.net - multiple datagridview data update to database mysql - vb.net 使用vb.net作为语言并将MySQL作为数据库将Excel加载到datagridview - Load excel to datagridview using vb.net as language and MySQL as Database VB.net 到 MySql 存储过程错误 - VB.net to MySql Stored Procedure Error 将数据从 vb.net web 表单保存到 mysql 适用于代码中明确说明的查询,但不适用于存储过程 - Saving data from a vb.net web form to mysql works with query explicitly stated in code, but not with stored procedure VB.net DataGridView 不显示来自 MySql 数据库的数据 - VB.net DataGridView not display the Data from MySql Data base
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM