DataGridView没有保存到数据库

时间:2015-06-02 12:53:31

标签: c# database datagridview insert sql-insert

我正在创建一个简单的人员目录程序,我正在创建它,以便用户可以更新目录。但是,在将数据添加到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);

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您应该添加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

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