C# 数据库合并/复制
我想为基于 MS Access 的应用程序创建一个复制解决方案。我有 2-4 个站点在数据库中创建内容,但它们都处于离线状态。到目前为止我的解决方案:
从文件菜单调用的函数:
class main
{
private void menuItemMergeUserData_Click(object sender, EventArgs e)
{
OpenFileDialog openDataFileDialog = new OpenFileDialog();
openDataFileDialog.Title = "Select UserData file to merge";
openDataFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
openDataFileDialog.Filter = "Database files (*.mdb)|*.mdb|All files (*.*)|*.*";
openDataFileDialog.RestoreDirectory = true;
if (openDataFileDialog.ShowDialog() == DialogResult.OK)
{
ExternalUserManager UserManager = new ExternalUserManager(openDataFileDialog.FileName, CompanyID, dbManager);
if (UserManager.MergeUserTables())
MessageBox.Show("UserData merged!", "UserData");
}
openDataFileDialog.Dispose();
}
}
在类ExternalUserManager中调用的函数
public bool MergeUserTables()
{
bool bOk = true;
try
{
//databaseManager.CallMacro(dbMyFileName);
conUserData.Open();
daClass.Fill(dsUserData, "Class");
daClassUsers.Fill(dsUserData, "ClassUsers");
daCompany.Fill(dsUserData, "Company");
daCurriculum.Fill(dsUserData, "Curriculum");
daCurriculumExercises.Fill(dsUserData, "CurriculumExercises");
daCurriculumMissions.Fill(dsUserData, "CurriculumMissions");
daCurriculumTasks.Fill(dsUserData, "CurriculumTasks");
daUserModes.Fill(dsUserData, "UserModes");
daUserProfile.Fill(dsUserData, "UserProfile");
daUsers.Fill(dsUserData, "Users");
bOk = databaseManager.MergeDataSet(dsUserData);
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Reading UserData");
}
finally
{
conUserData.Close();
}
return bOk;
}
在类DatabaseManager中调用的函数
public bool MergeDataSet(DataSetUserContent dsExternalUserContent)
{
bool bOk = false;
try
{
this.dsUserContent.MergeFailed += new MergeFailedEventHandler(OnMergeFailed);
this.dsUserContent.Merge(dsExternalUserContent);
if(this.dsUserContent.HasChanges())
bOk = SaveUsers();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "LogData Merge");
}
finally
{
conLogData.Close();
}
return bOk;
}
数据集按照我想要的方式合并,但是导入数据库中的新行没有保存到数据库???
寻找此问题的解决方案,并有人说设置 AcceptChangesDuringFill = false 。尝试过,但这使得导入数据库中的所有行都被添加为源数据库中的新行。
I want to create a replication solution for my MS Access based application. I have 2-4 sites creating content into the database, and they are all offline. My Solution so far:
Function called from File menu:
class main
{
private void menuItemMergeUserData_Click(object sender, EventArgs e)
{
OpenFileDialog openDataFileDialog = new OpenFileDialog();
openDataFileDialog.Title = "Select UserData file to merge";
openDataFileDialog.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
openDataFileDialog.Filter = "Database files (*.mdb)|*.mdb|All files (*.*)|*.*";
openDataFileDialog.RestoreDirectory = true;
if (openDataFileDialog.ShowDialog() == DialogResult.OK)
{
ExternalUserManager UserManager = new ExternalUserManager(openDataFileDialog.FileName, CompanyID, dbManager);
if (UserManager.MergeUserTables())
MessageBox.Show("UserData merged!", "UserData");
}
openDataFileDialog.Dispose();
}
}
Function called in class ExternalUserManager
public bool MergeUserTables()
{
bool bOk = true;
try
{
//databaseManager.CallMacro(dbMyFileName);
conUserData.Open();
daClass.Fill(dsUserData, "Class");
daClassUsers.Fill(dsUserData, "ClassUsers");
daCompany.Fill(dsUserData, "Company");
daCurriculum.Fill(dsUserData, "Curriculum");
daCurriculumExercises.Fill(dsUserData, "CurriculumExercises");
daCurriculumMissions.Fill(dsUserData, "CurriculumMissions");
daCurriculumTasks.Fill(dsUserData, "CurriculumTasks");
daUserModes.Fill(dsUserData, "UserModes");
daUserProfile.Fill(dsUserData, "UserProfile");
daUsers.Fill(dsUserData, "Users");
bOk = databaseManager.MergeDataSet(dsUserData);
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Reading UserData");
}
finally
{
conUserData.Close();
}
return bOk;
}
function called in class DatabaseManager
public bool MergeDataSet(DataSetUserContent dsExternalUserContent)
{
bool bOk = false;
try
{
this.dsUserContent.MergeFailed += new MergeFailedEventHandler(OnMergeFailed);
this.dsUserContent.Merge(dsExternalUserContent);
if(this.dsUserContent.HasChanges())
bOk = SaveUsers();
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "LogData Merge");
}
finally
{
conLogData.Close();
}
return bOk;
}
The DataSet is merged just as I want, however new rows from the imported database was not saved to database???
Have search for solutions for this problem and some say set AcceptChangesDuringFill = false . Tried it but this made all the rows from imported database be added as new rows in the source database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果所有应用程序都处于离线状态,为什么不直接将 mdb 文件复制过来呢?
我知道这很粗糙,但应该可以正常工作。
If all the apps are offline, why don't you simply copy the mdb file over?
It's crude, I know, but it should work fine.