C#中对datagridview的更改更新到数据库的语句

2024-12-17 09:52:06
推荐回答(2个)
回答1:

OleDbDataAdapter Ada = new OleDbDataAdapter();
                DataTable table = (DataTable)DataList.DataSource;
                //重新启动连接
                String ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Windows.Forms.Application.StartupPath + "/TEST.mdb";
                //用Buider方法更新数据
                using (OleDbConnection connection = new OleDbConnection(ConnectionString))
                {
                    Ada.SelectCommand = new OleDbCommand("SELECT * FROM TestACCESS", connection);
                    OleDbCommandBuilder builder = new OleDbCommandBuilder(Ada);
                    Ada.UpdateCommand = builder.GetUpdateCommand();
                    try
                    {
                        //更新数据表数据时
                        Ada.Update(table);        
       //----这个地方会把所有的更新同步到数据库中--------------------
                        table.AcceptChanges();
                        MessageBox.Show("操作已成功!数据将全部被保存......", "系统消息", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2);

                    }
                    }

回答2:

要写代码的,我给你个例子