c# - DataGridView 显示来自 MS Access 数据库的表。如何在 bindingNavigator1 中编写保存按钮以更新数据库?
问题描述
我整天都在谷歌上搜索这个。我有一个使用数据库自动填充某些字段的表单,但是数据库中的数据需要能够定期更新。对我们来说最简单的方法是允许用户更新 datagridview 中的字段。我只是找不到如何将更改保存到数据库。提前感谢您提供任何提示,我是新手,所以一直在谷歌搜索,这是我无法找到任何明确答案的第一个问题。
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Calculator
{
public partial class Database : Form
{
public Database()
{
InitializeComponent();
}
private void Database_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the
'tilesDataSet.Tiles_and_Quantities' table. You can move, or remove it, as needed.
this.tiles_and_QuantitiesTableAdapter.Fill(this.tilesDataSet.Tiles_and_Quantities);
}
private void runtimeCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form1 = new Form1();
form1.Show();
}
private void standardCalculatorToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form3 = new Form3();
form3.Show();
}
private void savedResultsToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Hide();
var form2 = new Form2();
form2.Show();
}
private void tiles_and_QuantitiesBindingNavigatorSaveItem_Click(object sender,
EventArgs e)
{
}
private void tiles_and_QuantitiesDataGridView_CellContentClick(object sender,
DataGridViewCellEventArgs e)
{
}
}
}
```
我知道这有很多例子,但主要是使用 SQL,而且我使用的是 Access 数据库。仍然有一些例子表明了这一点,但我对此很陌生,并且正在努力实施它们。目前,我正在尝试:
```
private void saveToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.Validate();
this.tiles_and_QuantitiesBindingSource.EndEdit();
this.tiles_and_QuantitiesTableAdapter.Update(this.tilesDataSet.Tiles_and_Quantities);
MessageBox.Show("Update successful");
}
catch (System.Exception ex)
{
MessageBox.Show("Update failed");
```
并且在 .Update() 中收到错误 CS1061。再次,任何帮助将不胜感激。
解决方案
推荐阅读
- haskell - Haskell 中 Equals 的基本实现
- powershell - 使用PowerShell匹配比较多个电子邮件地址
- javascript - How to handle AJAX when PHP code fails but sends 200?
- python - 如何在不改变段顺序的情况下打乱数据段?
- r - 如何在 R 中创建一个循环或函数来选择所需的数据?我的数据存储为列表
- javascript - django-leaflet + leaflet-rastercoord (macOS 10.13.6 + homebrew)
- multithreading - CGAL 2D 正则化布尔集操作 lib 线程安全吗?
- typescript - 打字稿:调用函数时如何要求类型规范?
- python - 如何使用 matplotlib 在 python 中绘制 3D 权重函数
- javascript - 用于匹配括号中字符串的正则表达式,包括缺少左括号或右括号时