【问题标题】:DataGridView not saving to databaseDataGridView 不保存到数据库
【发布时间】:2015-06-02 12:53:30
【问题描述】:

我正在创建一个简单的员工目录程序,并且我正在制作它以便用户可以更新目录。但是,当前将数据添加到 DataGridView 时,它不会保存到数据库中。我知道需要this.staffTableAdapter.Update(this.dBDataSet1.Staff);,它在我的程序中,如下所示。

我只是不确定我目前缺少什么,如果有人可以帮助我,我将非常感激。

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 StaffDirectory
{
    public partial class Administration : Form
    {
        public Administration()
        {
            InitializeComponent();
        }

        private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.staffBindingSource.EndEdit();
            this.staffTableAdapter.Update(this.dBDataSet1.Staff);
        }

        private void Administration_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dBDataSet1.Staff' table. You can move, or remove it, as needed.
            this.staffTableAdapter.Fill(this.dBDataSet1.Staff);

        }
    }
}

【问题讨论】:

  • 那里没有例外?
  • 有时您可能会使用数据库的副本。确保你不工作。您可以通过数据库的属性来检查它。我不确定你是否做到了。
  • 我已经从我的数据库中创建了一个数据集,在这种情况下“whit”是什么意思?

标签: c# database datagridview insert sql-insert


【解决方案1】:

您应该添加try-catch 块并分析/向我们提供异常描述(ex.Message):

private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
    try{
       this.Validate();
       this.staffBindingSource.EndEdit();
       this.staffTableAdapter.Update(this.dBDataSet1.Staff);
    }
    catch(Exception ex) {}
}

另外,添加this.dBDataSet1.Staff.AcceptChanges(); (回复:https://msdn.microsoft.com/en-us/library/ceab2k93.aspx

希望这可能会有所帮助。最好的问候,

【讨论】:

    猜你喜欢
    • 2011-02-06
    • 2013-08-03
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2013-03-30
    相关资源
    最近更新 更多