MySQL 和 C#.NET DataGridView、更新、插入和删除
前几天,我开始使用 Windows 窗体在 C#.NET 中开发一个项目,自从我上次使用 Windows 窗体以来已经有一段时间了,所以我感觉有点生疏。
首先,我应该指出,我使用的是 Visual C# 2010 Express,它没有对数据绑定的 GUI 向导支持(如果您喜欢 MS Access 或本地存储的数据库文件,它确实支持),并且我需要与 MySQL 数据库进行交互让它与 DataGridView 配合良好。
我的问题源于这样一个事实:我不太清楚如何进行更新和删除。我发现的大多数在线资源似乎只是指运行数据绑定向导,这是我无法做到的(因为我正在运行 Visual C# 2010 Express)。
现在,我已经能够从数据库中选择数据,没有任何重大问题(使用 MySqlDataAdapter
、BindingSource
和 DataTable
),但是当我尝试使用 MySqlDataAdapter.Update(
时什么也没有发生。
我假设我需要做一些设置工作,这些工作通常会被视为“只需使用向导”。那么,有没有人有一个简单的例子来说明我需要做什么才能进行更新、插入和删除?无论有没有存储过程,两种方法都可以(尽管我最终可能会在完成的程序中使用存储过程)。
The other day I started working on a project in C#.NET using Windows Forms and it's been quite a while since I last used Windows Forms so I'm feeling a bit rusty.
First off I should point out that I'm using Visual C# 2010 Express which does not have GUI wizard support for databindings (well it does, if you enjoy MS Access or locally stored database files) and I need to speak to a MySQL database and make it play nice with a DataGridView.
My problem stems from the fact that I can't quite figure out how to do updates and deletes. Most online sources I've found seem to just refer to running the data binding wizard, something I can't do (since I'm running Visual C# 2010 Express).
Now, I've been able to select data from the database without any major problems (using a MySqlDataAdapter
, a BindingSource
and a DataTable
) but when I try to use MySqlDataAdapter.Update(<DataTable>)
nothing happens.
I'm assuming there is some setup work I need to do which is normally handwaved away as "just use the wizard". So, does anyone have a simple example of what I need to do to be able to do updates, inserts and deletes? With or without stored procedures, both ways are fine (although I'll probably end up used stored procs in the finished program).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议您不要使用 VS Toolbox 中的那些 DragDroppable DataAdapter 或 BindingSources,只需将您的项目正确地分层到不同的项目中,每个项目都有自己的关注点,例如数据访问、业务逻辑和演示(GUI)。
请参阅我的答案:MVC3 和实体框架
如果你有一个坚实的分层/在 m 层架构中,您会发现数据管理逻辑仅属于数据访问层,UI 只需通过业务逻辑询问需要什么。
I suggest you to do not use those DragDroppable DataAdapter or BindingSources from the VS Toolbox, just layer your project properly in different projects each one with its own concern, like Data Access, Business Logic and Presentation (GUI).
see my answer here: MVC3 and Entity Framework
if you have a solid layered / m-tier architecture you will find out that data management logic belongs only to the Data Access Layer and the UI just has to ask what is needed going through the Business Logic.