|
/update source to dest
if (setScriptsIndex > 0)
{
cmd = new SqlCommand("Update " + tableName + " set " + setScripts + " where " + primaryKeyName + "='" + recordsSource[primaryKeyValue][primaryKeyName] + "'", destinationConnector);
cmd.ExecuteNonQuery();
}
//insert source to dest
if (insertScriptsIndex > 0)
{
cmd = new SqlCommand("insert into " + tableName + " (" + insertKeysScripts + ") values (" + insertValuesScripts + ")", destinationConnector);
cmd.ExecuteNonQuery();
}
}
//after update and insert, the count still not match, means we delete some records in source db, then we also need to delete the records in destination db
foreach (var re in recordsDest)
{
//get the delete record primary key value
if (!recordsSource.ContainsKey(re.Key))
{
cmd = new SqlCommand("delete from " + tableName + " where " + primaryKeyName + "='" + re.Value[primaryKeyName].ToString() + "'", destinationConnector);
cmd.ExecuteNonQuery();
}
}
// Close objects
destinationConnector.Close();
mySqlConn.Close();
}
代码的基础类其他部分请看下列文章:
1. C#同步SQL Server数据库中的数据--数据库同步工具[同步已有的有变化的数据]
2.分析下自己写的SQL Server同步工具的性能和缺陷
3.C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据] 4.C#同步SQL Server数据库Schema
|