DataGridView已更新,但Microsoft Access数据库未更新 [英] DataGridView Updated But Microsoft Access Database Not
问题描述
嗨
按保存时,数据在数据视图中更改但访问文件中没有任何更改。
请帮助
使用System;
使用System.Collections.Generic;使用System.ComponentModel
;
使用System.Data;使用System.Drawing
;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.OleDb;
名称空间BUDBProject
{
公共部分类BUDBForm1:表格
{
public BUDBForm1()
{
InitializeComponent() ;
}
//加载Form1
private void Form1_Load(对象发送者,EventArgs e)
{
// TODO:这行代码将数据加载到'bUDBACDataSet .BUDBT1'表。您可以根据需要移动或删除它。
this.bUDBT1TableAdapter.Fill(this.bUDBACDataSet.BUDBT1);
//加载时禁用InformationPanel
InformationPanel.Enabled = false;
}
// END加载Form1
//开始新的Buttom
private void NewButt_Click(object sender,EventArgs e)
{
//启用Pannel
InformationPanel.Enabled = true;
//输入数据
尝试
{
CompanyTB.Focus();
this.bUDBACDataSet.BUDBT1.AddBUDBT1Row(this.bUDBACDataSet.BUDBT1.NewBUDBT1Row());
bUDBT1BindingSource.MoveLast();
}
catch(exception ex)
{
MessageBox.Show(ex.Message," Message",MessageBoxButtons.OK,MessageBoxIcon.Error);
bUDBT1BindingSource.ResetBindings(false);
}
}
// END新按钮
//开始编辑按钮
private void EditButt_Click(object sender,EventArgs e)
{
InformationPanel.Enabled = true;
}
//结束编辑按钮
//开始保存按钮
private void SaveButt_Click(object sender,EventArgs e)
{
//禁用面板
InformationPanel.Enabled = false;
//保存数据
尝试
{
this.Validate();
this.bUDBT1BindingSource.EndEdit();
this.bUDBACDataSet.AcceptChanges();
this.bUDBT1TableAdapter.Update(this.bUDBACDataSet.BUDBT1);
}
catch(exception ex)
{
MessageBox.Show(ex.Message," Message",MessageBoxButtons.OK,MessageBoxIcon。错误);
bUDBT1BindingSource.ResetBindings(false);
}
}
//结束保存按钮
//开始取消按钮
private void CancButt_Click(object sender,EventArgs e)
{
//禁用Panel
InformationPanel.Enabled = false;
bUDBT1BindingSource.ResetBindings(false);
}
//结束取消按钮
}
}
确保在属性下复制到输出目录到"如果更新则复制"这意味着第一次构建的数据库是复制到调试文件夹的,除非您修改数据库,否则永远不会再复制。默认值为"始终复制"这意味着每个构建的
数据库被复制到调试文件夹,消除了先前构建的所有更改。
另请参阅:MSDN
使用复制到输出目录文章 /代码示例。 MSDN: 管理项目中的本地数据文件
Hi
when press save , data changed in datagridview but nothing changed in access file.
Please Help
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.OleDb; namespace BUDBProject { public partial class BUDBForm1 : Form { public BUDBForm1() { InitializeComponent(); } //Load Form1 private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'bUDBACDataSet.BUDBT1' table. You can move, or remove it, as needed. this.bUDBT1TableAdapter.Fill(this.bUDBACDataSet.BUDBT1); //Disable InformationPanel On Load InformationPanel.Enabled = false; } //END Load Form1 //Start NEW Buttom private void NewButt_Click(object sender, EventArgs e) { //Enable Pannel InformationPanel.Enabled = true; //Enter Data try { CompanyTB.Focus(); this.bUDBACDataSet.BUDBT1.AddBUDBT1Row(this.bUDBACDataSet.BUDBT1.NewBUDBT1Row()); bUDBT1BindingSource.MoveLast(); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); bUDBT1BindingSource.ResetBindings(false); } } //END New Button //Start Edit Buttom private void EditButt_Click(object sender, EventArgs e) { InformationPanel.Enabled = true; } //End Edit Button //Start Save Button private void SaveButt_Click(object sender, EventArgs e) { //Disable Panel InformationPanel.Enabled = false; //Save Data try { this.Validate(); this.bUDBT1BindingSource.EndEdit(); this.bUDBACDataSet.AcceptChanges(); this.bUDBT1TableAdapter.Update(this.bUDBACDataSet.BUDBT1); } catch (Exception ex) { MessageBox.Show(ex.Message, "Message", MessageBoxButtons.OK, MessageBoxIcon.Error); bUDBT1BindingSource.ResetBindings(false); } } //End Save Button //Start Cancel Button private void CancButt_Click(object sender, EventArgs e) { //Disable Panel InformationPanel.Enabled = false; bUDBT1BindingSource.ResetBindings(false); } //End Cancel Button } }
Make sure under properties to Copy to Output Directory to "Copy if newer" which means the database on the first build is copies to the debug folder and never again unless you modify the database. The default is "Copy always" which means on each build the database is copied to the debug folder wiping out all changes from the prior build.
See also: MSDN Working with Copy to Output Directory article/code sample. MSDN: Manage Local Data Files in Your Project
这篇关于DataGridView已更新,但Microsoft Access数据库未更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!