0

我正在尝试将 DataGridview 中的更改保存回 SQL Server 2008。我有以下代码,但它仅在第一次单击按钮时更新数据库。我在哪里俯瞰什么?

private void btnDone_Click_1(object sender, EventArgs e)
{
    try
    {
       SqlConnection con = new SqlConnection(constr);
       con.Open();

       adapter.SelectCommand = new SqlCommand("SELECT * FROM trips", con);
       SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
       adapter.Fill(dt);
       adapter.Update(dt);

       con.Close();

       MessageBox.Show("Changes Saved", "Updating...", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
    catch (Exception)
    {
       throw;
    }
    finally { lblCount.Text = "Total Records Displayed: " + dgvTrips.Rows.Count.ToString(); }
}
4

1 回答 1

0

通过声明一个全局 SqlDataAdapter 来工作

于 2016-09-15T08:50:59.263 回答