如何在C#中将多个记录从DataGridview保存到DB中 [英] How to save multiple records from the DataGridview into DB in C#

查看:449
本文介绍了如何在C#中将多个记录从DataGridview保存到DB中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个datagridview视图,如下所示:



ProductId |名称|价格|数量



datagridview以所有单元格为空开始。



用户将在每个单元格中输入数据列。用户输入的产品数量发生变化(未知)。用户只能输入一种产品或五种产品。



因此,一旦用户输入了他必须输入的所有产品,我希望当他点击一个按钮时,所有产品都将保存到数据库中。



SQL数据库中的表格看起来像数据网格:



ProductId |名称|价格|数量



由于用户输入的产品数量未知,如何将数据保存到数据库?



大喊大叫删除[/编辑]

I have a datagridview view that looks like this:

ProductId | Name | Price | Quantity

The datagridview starts with all cells empty.

The user will enter data in each of the columns. The number of products the user enters changes(is unknown). The user could enter only one product or five products.

So, once the user has entered all the products he has to enter, i want that when he clicks a button, all products will be saved to the database.

The table in the SQL database looks like the datagrid:

ProductId | Name | Price | Quantity

As the number of products that the user will enter is unknown, how can I save the data to the database?

Shouting removed[/Edit]

推荐答案

只需将每个产品视为一个单独的项目,然后循环浏览每个需要的产品插入并做到了。

处理多行与处理单个数据行几乎完全相同。唯一的区别是在某种循环中处理多行。
Just treat each product as an individual item and just loop through each product that needs to be inserted and do it.
Dealing with multiple rows is pretty much exactly the same as dealing with individual rows of data. The only difference is that multiple rows are dealt with in a loop of some kind.


在datagridview中插入新行时将该行的值保存在datatable中,然后将该数据表保存到viewstate [dt]。在每个插入新行(即插入命令)时,使用保存在viewstate [dt]中的数据表绑定datagridview。你的所有数据都在viewstate [dt]中。你可以在这个上循环viewState [dt]在数据库中添加保存数据。
On inserting new row in datagridview save that row''s value in datatable and then save that datatable to viewstate["dt"].On each insert new row( i.e. insert command), Bind datagridview with datatable which saved in viewstate["dt"] .your all data come in the viewstate["dt"].You can make loop on this viewState["dt"] to add save data in database.


这篇关于如何在C#中将多个记录从DataGridview保存到DB中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆