从DataGridView更新数据库 [英] Update a database from DataGridView

查看:59
本文介绍了从DataGridView更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯于在家里酿造很多这样的DAO东西,但在这种情况下我想尽可能多地使用Visual Studio中的DataSet和内置功能,因为这是一个非常基本的应用程序类型。以下是我对
申请的基本要求:


1。用户将在运行时从drowdown中选择一个环境(数据库是相同的,即Prod,QA,UAT,用户需要能够选择他们将在哪个环境中工作);


2。用户将显示一个记录列表,表示他们可以修改的计划任务(更新,添加,删除)。


为此,我创建了一个SQL proc来带回所有记录在数据库中,以及插入和更新过程。我想使用proc而不是生成的UPDATE语句,因为用户的名称以及当前的DateTime需要保留
作为添加或更新的记录的一部分。我在Visual Studio中创建了一个DataSet,并将我包含的主表(BULK_DATA_UPLOAD_SCHEDULE)拖到窗体上。我有一个DataGridView和一个带有ToolStripButton的BindingNavigator,大概是
,用于处理DataGridView的CRUD操作。


当应用程序运行时,所有内容都可以很好地加载到DataGridView中。通过更新TableAdapter上的ConnectionString,我也能够改变环境......到目前为止,非常好......


问题似乎当我使用BindingNavigator上的按钮尝试添加,更新或删除行 时进来。 BindingNavigator和DataGridView似乎绑定在一起,因为单击Delete按钮确实
从tabl中删除一行; "添加"按钮创建一个新行,导航按钮起作用。但是,没有任何内容持久存储到数据库中。此外,当添加新行时,行中的值不准确,因为需要设置
的默认值(一列,BULK_DATA_OBJECT_ID,是另一个talbe的外键引用。我有将此列绑定到DropDownList,该表在使用现有值加载时正确填充,但在新行上,未选择它的值。
默认值应该在此处自动填充,不知何故)。所以,我想我的问题是:


1)如何将DataGridView的值返回到数据库?请记住,我想为这些调用我自己的插入/更新过程。我知道如何将它们添加到deisgneer中的TableAdapter,但是有一些事件我应该在TableAdpater上绑定
吗? DataGridView?


2)与#1一起使用,当用户添加新行时,如何指定DataGridView中新行上显示的默认值? / p>

3)我想在某些时候做一些基本的验证beofre数据只是被推回到数据库(例如,我想确保没有任何时间表重叠。什么事件以及我需要监听什么对象所以我可以在数据进入数据库之前执行此验证


4)最后,当用户退出时应用程序,我想确保DataGridView不脏。我需要做些什么才能找到这个?


感谢您的帮助。这一切似乎都是非常基本的东西,应该是对这些对象的演练,但是如果有的话,我有一段时间试图找到它。我能够在BindingNavigator的MenuItemToolStrip
上的Save按钮上捕获Click事件,然后在我的TableAdapter上调用Update(dataset),传入我创建的DataSet。这确实有效,但我不完全确定如何或为什么,它显然是调用内置的Update函数,我不想用于上述
的原因。它也是perofrms没有验证,我想在允许它更新之前再做。

解决方案

嗨詹姆斯,


1)通过ADO.NET调用插入/更新过程:


在ADO.NET中调用存储过程


2)DataTable为每列都有一个DefaultValue属性:

 dt.Columns [0] .DefaultValue = -1; 

3)请按照此MSDN文档中的示例:


如何:验证Windows窗体DataGridView控件中的数据


4)请检查此事件:


DataGridView CurrentCellDirtyStateChanged
Event


I'm used to home-brewing a lot of this DAO stuff, but in this case I'm wanting to use the DataSet and built-in functionality from Visual Studio as much as possible, as this is a very basic utility type of application. Here are my basic requirements for the application:

1. User will select an environment at runtime from a drowdown (The databases are identical, i.e., Prod, QA, UAT, and user needs to be able to choose which environment they will be working in);

2. User will be displayed a list of records representing scheduled tasks which they can modify (Update, Add, Delete).

To this end, I have created a SQL proc to bring backk all the records in the database, as well as an Insert and Update proc. I want to use procs instead of generated UPDATE statements because the user's name as well as the current DateTime need to be persisted as part of records that are added or updated. I created a DataSet in Visual Studio, and dragged the main table that I included (BULK_DATA_UPLOAD_SCHEDULE) onto the form. I got a DataGridView and a BindingNavigator with ToolStripButton's that are presumably meant to handle the CRUD operations of the DataGridView.

When the application runs, everything loads just fine into the DataGridView. I was also able to get the changing environment to work by updating the ConnectionString on the TableAdapter... So far, so good...

The problem seems to come in when I use the buttons on the BindingNavigator to attempt to add, update, or delete rows. The BindingNavigator and DataGridView appear to be bound together, because clicking the Delete button does indeed remove a row from the tabl; the Add button creates a new row, and the navigation buttons work. However, nothing gets persisted to the database. Furthermore, when a new row is added, the values aren't accurate in the row, because there are default values that need to be set (One column, BULK_DATA_OBJECT_ID, is a foreign key reference to another talbe. I have bound this column to a DropDownList that is correctly populated when the table is loaded with existing values, but on a new row, it's value is not selected. A default value should be getting auto-populated here, somehow). So, I guess my questions are these:

1) How do I get the values form the DataGridView back to the database? Bear in mind that I want to call my own Insert / Update procs for these. I know how to add them to the TableAdapter in the deisgneer, but is there some event that I'm supposed to be binding to on the TableAdpater? The DataGridView?

2) Hand-in-hand with #1, how do I specifiy the default values to appear on a new row in the DataGridView when the user adds a new row?

3) I'd like to do some basic validation at some point beofre data is just pushed back to the database (for example, I'd like to make sure that none of the schedules overlap. What event and on what object do I need to listen so I can perform this validation before the data goes to the database?

4) Lastly, when the user exits the application, I'd like to make sure that the DataGridView isn't dirty. What do I need to do to find this?

Thanks for any assistance. This all seems like very basic stuff that should have been a walkthrough on these objects, but I have had an awful time trying to find it if it is. I was able to capture the Click event on the Save button on the MenuItemToolStrip of the BindingNavigator, and then call Update( dataset) on my TableAdapter, passing in my DataSet I'd created. This did work, but I'm not entirely sure how or why, and it obviously is calling the built-in Update functions, which I didn't want to use for the aforemetioned reasons. It also perofrms no validation, which again, I want to do before allowing it to update.

解决方案

Hi James,

1) Call the Insert/Update procedure via ADO.NET:

Calling Stored procedures in ADO.NET

2) DataTable has a DefaultValue property for every column:

dt.Columns[0].DefaultValue = -1; 

3) Please follow the example in this MSDN document:

How to: Validate Data in the Windows Forms DataGridView Control

4) Please check this event:

DataGridView.CurrentCellDirtyStateChanged Event


这篇关于从DataGridView更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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