从对多个数据网格的更改更新本地存储的数据库(Visual studio C#,使用 OLEDB 访问数据库)
Updating locally stored database from changes to multiple data grids ( Visual studio C#, Access database using OLEDB)
我正在尝试根据包含数据库副本的数据网格发生的更改来更新本地存储的数据库,但失败很大。我认为我遇到问题的原因是数据库有两个表,我已将它们放入两个数据网格中,但所有数据集都是一个数据集。我对 OLEDB 的了解有限,但我已经创建了多个具有更新例程的程序,如下所示,所以我不确定如何适应这个新程序。
Screenshot of tables in working program
变量
OleDbConnection Connection;
OleDbDataAdapter oledbAdapter;
OleDbCommandBuilder oledbCmdBuilder;
//DataSet ds = new DataSet();
string ConnectionString = null;
int CurrentRow = 0;
DataSet ds = new DataSet();
数据库连接 - 在加载时执行
private void database_datagrid_load()
{
string SQL1 = "SELECT * FROM tbl_Customers";
string SQL2 = "SELECT * FROM tbl_Jobs";
Connection = new OleDbConnection(ConnectionString);
try
{
ds.Clear();
Connection.Open();
oledbAdapter = new OleDbDataAdapter(SQL1, Connection);
oledbAdapter.Fill(ds, "tbl_Customers");
oledbAdapter.SelectCommand.CommandText = SQL2;
oledbAdapter.Fill(ds, "tbl_Jobs");
oledbAdapter.Dispose();
Connection.Close();
database_datagrid_customer.DataSource = ds.Tables[0];
database_datagrid_jobs.DataSource = ds.Tables[1];
}
catch(Exception ex)
{
MessageBox.Show(Convert.ToString(ex));
}
}
只有未崩溃的更新代码 - 它已完成但未对数据库进行任何更改
Connection.Open();
string SQL = "SELECT * FROM tbl_Jobs";
OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now
OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW); //cmdbuilder is set but never used not sure why
DataSet changes = ds.GetChanges();
if (changes != null)
{
oledbAdapterNEW.Update(ds.Tables[0]);
}
ds.AcceptChanges();
MessageBox.Show("Jobs Save Changes");
不是积极的,但我认为应该是
if (changes != null)
{
ds.Tables[0].AcceptChanges();
//oledbAdapterNEW.Update(ds.Tables[0]);
}
想通了,这是给有需要的人的解决方案。更新两个表。
private void database_btn_updatedb_Click(object sender, EventArgs e)
{
Connection = new OleDbConnection(ConnectionString);
DataSet changes = ds.GetChanges();
try
{
Connection.Open();
string SQL = "SELECT * FROM tbl_Jobs";
OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now
OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW);
if (changes != null)
{
oledbAdapterNEW.Update(ds,"tbl_Jobs");
MessageBox.Show("Jobs Save Changes");
}
SQL = "SELECT * FROM tbl_Customers";
oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection);
oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW);
if (changes != null)
{
oledbAdapterNEW.Update(ds, "tbl_Customers");
MessageBox.Show("Customer Save Changes");
}
ds.AcceptChanges();
Connection.Close();
}
我正在尝试根据包含数据库副本的数据网格发生的更改来更新本地存储的数据库,但失败很大。我认为我遇到问题的原因是数据库有两个表,我已将它们放入两个数据网格中,但所有数据集都是一个数据集。我对 OLEDB 的了解有限,但我已经创建了多个具有更新例程的程序,如下所示,所以我不确定如何适应这个新程序。
Screenshot of tables in working program
变量
OleDbConnection Connection;
OleDbDataAdapter oledbAdapter;
OleDbCommandBuilder oledbCmdBuilder;
//DataSet ds = new DataSet();
string ConnectionString = null;
int CurrentRow = 0;
DataSet ds = new DataSet();
数据库连接 - 在加载时执行
private void database_datagrid_load()
{
string SQL1 = "SELECT * FROM tbl_Customers";
string SQL2 = "SELECT * FROM tbl_Jobs";
Connection = new OleDbConnection(ConnectionString);
try
{
ds.Clear();
Connection.Open();
oledbAdapter = new OleDbDataAdapter(SQL1, Connection);
oledbAdapter.Fill(ds, "tbl_Customers");
oledbAdapter.SelectCommand.CommandText = SQL2;
oledbAdapter.Fill(ds, "tbl_Jobs");
oledbAdapter.Dispose();
Connection.Close();
database_datagrid_customer.DataSource = ds.Tables[0];
database_datagrid_jobs.DataSource = ds.Tables[1];
}
catch(Exception ex)
{
MessageBox.Show(Convert.ToString(ex));
}
}
只有未崩溃的更新代码 - 它已完成但未对数据库进行任何更改
Connection.Open();
string SQL = "SELECT * FROM tbl_Jobs";
OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now
OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW); //cmdbuilder is set but never used not sure why
DataSet changes = ds.GetChanges();
if (changes != null)
{
oledbAdapterNEW.Update(ds.Tables[0]);
}
ds.AcceptChanges();
MessageBox.Show("Jobs Save Changes");
不是积极的,但我认为应该是
if (changes != null)
{
ds.Tables[0].AcceptChanges();
//oledbAdapterNEW.Update(ds.Tables[0]);
}
想通了,这是给有需要的人的解决方案。更新两个表。
private void database_btn_updatedb_Click(object sender, EventArgs e)
{
Connection = new OleDbConnection(ConnectionString);
DataSet changes = ds.GetChanges();
try
{
Connection.Open();
string SQL = "SELECT * FROM tbl_Jobs";
OleDbDataAdapter oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection); //new adapter with just jobs table ignoring customers for now
OleDbCommandBuilder oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW);
if (changes != null)
{
oledbAdapterNEW.Update(ds,"tbl_Jobs");
MessageBox.Show("Jobs Save Changes");
}
SQL = "SELECT * FROM tbl_Customers";
oledbAdapterNEW = new OleDbDataAdapter(SQL, Connection);
oledbCmdBuilderNEW = new OleDbCommandBuilder(oledbAdapterNEW);
if (changes != null)
{
oledbAdapterNEW.Update(ds, "tbl_Customers");
MessageBox.Show("Customer Save Changes");
}
ds.AcceptChanges();
Connection.Close();
}