【发布时间】:2020-07-25 22:13:41
【问题描述】:
我的表单中有一个 DataGridView,我有一个保存按钮。 DataAdapter 和DataSet 都是自动生成的。 我想使用 DataAdapter.Update() 来更新我的数据库,但是当我在 .mdf 中打开表或再次生成解决方案时更新 DataGridView 后似乎没有任何变化。
我知道有人问过这个问题并阅读了一些帖子,试图找到解决方案但它不起作用。
- 我已将 .mdf 文件属性“复制到输出目录”设置为“如果较新则复制”
- BindingSource 和 BindingNavigator 工作成功。
代码示例
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;
using System.Data.SqlClient;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.myTableTableAdapter.Fill(this.myDatabaseDataSet.myTable);
SqlCommandBuilder sqlCommandBuilder = new SqlCommandBuilder(myTableTableAdapter.Adapter);
myTableTableAdapter.Adapter.InsertCommand = sqlCommandBuilder.GetInsertCommand();
myTableTableAdapter.Adapter.DeleteCommand = sqlCommandBuilder.GetDeleteCommand();
myTableTableAdapter.Adapter.UpdateCommand = sqlCommandBuilder.GetUpdateCommand();
}
private void SaveSToolStripButton_Click(object sender, EventArgs e)
{
try
{
bindingSource1.EndEdit();
myTableTableAdapter.Adapter.Update(myDatabaseDataSet.myTable);
MessageBox.Show("Succeed");
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Failed");
}
}
}
}
【问题讨论】:
标签: c# sql-server winforms datagridview ado.net