0

编辑网格视图后无法更新对数据库的更改。没有抛出错误,但数据库中的表中的更改没有更新。我在这里使用 Sql 服务器,我有两个名为 Form1 和 Form 2 的 Winform。一个按钮(Form1 中的 btnSearchAll)打开 Form2 中的 gridview 并加载所有数据。当我尝试在 gridview(在 Form2 中)编辑数据并尝试更新时,它不会更新更改。请帮忙

下面是我的表格1:

    namespace Tailor_app
{
    public partial class Form1 : Form
    {
        DataSet ds = new DataSet();
        SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
        SqlDataAdapter da;
        DataTable dt;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
        txtFirstName.Focus();
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
            DataRow dr = dt.NewRow();
            dr["CellNumber"] = txtCellNo.Text.Trim();
            dr["FirstName"] = txtFirstName.Text;
            dr["LastName"] = txtLastName.Text;
            dr["Shirt"] = txtShirt.Text;
            dr["Pant"] = txtPant.Text;
            dr["DueDate"] = txtDueDate.Text;
            dr["Date"] = txtDate.Text;
            if (dr["CellNumber"] == "")
            {
                MessageBox.Show("Please enter Cell Number");
            }
            else if (dr["CellNumber"] != "")
            {
                dt.Rows.Add(dr);
                MessageBox.Show("Success");
            }

                try
                {
                    da.Update(ds, "Measurement");
                }
                catch (DBConcurrencyException ex)
                {
                    MessageBox.Show(ex.Message);
                    dt.Clear();
                    da.Fill(ds, "Measurement");
                }
        }

    private void btnSearchAllCustomers_Click(object sender, EventArgs e)
    {
        this.Hide();
        frmDgv_SearchResult frm2 = new frmDgv_SearchResult();
        frm2.Show();

        using (da = new SqlDataAdapter())
        {
            try
            {
                da.SelectCommand = new SqlCommand("Select * From Measurement", con);
                ds.Clear();
                da.Fill(ds,"Measurement");
                dt = ds.Tables["Measurement"];
                frm2.dgvSearchResults.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }
}

}

下面是我的表格2:

namespace Tailor_app
{

public partial class frmDgv_SearchResult : Form
{
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection("server=(local);Database=MSNETDB;Integrated Security=true;");
    SqlDataAdapter da;
    DataTable dt;

    public frmDgv_SearchResult()
    {
        InitializeComponent();
    }

    private void dgvSearchResults_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {

    }

      private void frmDgv_SearchResult_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        try
            {
                da.Update(ds, "Measurement");
            }
            catch (DBConcurrencyException ex)
            {
                MessageBox.Show(ex.Message);
                dt.Clear();
                da.Fill(ds, "Measurement");
            }
            finally
            {
                MessageBox.Show("success");
            }
        }
4

3 回答 3

1

您应该dt.AcceptChanges();在插入、更新或删除之后。AcceptChanges提交自上次AcceptChanges调用以来对数据表所做的所有更改

于 2013-02-04T15:15:05.300 回答
0

尝试为您的数据适配器请求更新命令:-

  private void frmDgv_SearchResult_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("select * from Measurement", con);
        da.Fill(ds, "Measurement");
        dt = ds.Tables["Measurement"];
        SqlCommandBuilder cb = new SqlCommandBuilder(da);

        cb.GetInsertCommand();
        cb.GetUpdateCommand();
        cb.GetDeleteCommand();

        cb.ConflictOption = ConflictOption.CompareAllSearchableValues;
    }
于 2013-02-04T15:49:30.327 回答
0

我的看起来更像这样:

da.Fill(dt)

Dim cb As SqlCommandBuilder = New SqlCommandBuilder(da)

da.MissingSchemaAction = MissingSchemaAction.AddWithKey     ' includes default field value settings
da.InsertCommand = cb.GetInsertCommand      ' instantiate for transaction
da.UpdateCommand = cb.GetUpdateCommand
于 2013-02-04T18:26:05.067 回答