数据库

 首页 > 数据库 > MySql > SQL Server数据全同步及价值分析[终结版]SQL Server数据全同步[终结版]1. C#同步SQL Server数据库中的数据--数据库同步工具[同步已有的有变化的数据]       2.分析下自己写的SQL Server同步工具的性能和缺陷              3.C#同步SQL Server数据库中的数据--数据库同步工具[同步新数据]             

SQL Server数据全同步及价值分析[终结版]SQL Server数据全同步[终结版]1. C#同步SQL Server数据库中的数据--数据库同步工具[同步已有的有变化的数据]       2.分析下自己写的SQL Server同步工具的性能和缺陷...

分享到:
【字体:
导读:
         摘要:SQL Server数据全同步及价值分析[终结版] ...

SQL Server数据全同步及价值分析[终结版]

SQL Server数据全同步[终结版]

版权所有,转载请注明出处,谢谢!

经过两天的同步编写和测试,出了第一个Release版本:

1. 本函数仅支持单向同步,即从一个主数据库想多个从数据库同步

2.主数据库的任何增删改都会同步到所有从数据库上

3. 最重要的一点:同步数据库的价值所在:当主数据库服务器不可用时,程序可以使用其他从数据库或者备用数据库,这对于未来公有云和私有云应用具有重大价值!

代码:

/// 
        /// Note: for columns, the first string must be primary key name!
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        public void BulkUpdateTo(string server, string database, string uid, string password, string tableName, List columns, List ignoreUpdateColumns, List ignoreInsertColumns)
        {
            string primaryKeyName = columns[0];
            string connectionString = "Server=" + server + ";Database=" + database + ";User Id=" + uid + ";Password=" + password;
            // Create destination connection
            SqlConnection destinationConnector = new SqlConnection(connectionString);

            SqlCommand cmd = new SqlCommand("SELECT * FROM " + tableName, destinationConnector);
            // Open source and destination connections.
            this.EnsureConnectionIsOpen();
            destinationConnector.Open();

            Dictionary Index_PrimaryKeyValue = new Dictionary();

            SqlDataReader readerSource = cmd.ExecuteReader();
            Dictionary> recordsDest = new Dictionary>();
            int i = 0;
            while (readerSource.Read())
            {
                Index_PrimaryKeyValue.Add(i, readerSource[primaryKeyName].ToString());
                string recordIndex = Index_PrimaryKeyValue[i];
                recordsDest[recordIndex] = new Dictionary();
                foreach (string keyName in columns)
                {
                    recordsDest[recordIndex].Add(keyName, readerSource[keyName].ToString());
                }
                i++;
            }

            // Select data from Products table
            cmd = new SqlCommand("SELECT * FROM " + tableName, mySqlConn);
            // Execute reader
            SqlDataReader reader = cmd.ExecuteReader();
            Dictionary> recordsSource = new Dictionary>();

            Dictionary Index_PrimaryKeyValue2 = new Dictionary();

            int j = 0;
            while (reader.Read())
            {
                Index_PrimaryKeyValue2.Add(j, reader[primaryKeyName].ToString());
                string recordIndex = Index_PrimaryKeyValue2[j];
                recordsSource[recordIndex] = new Dictionary();
                foreach (string keyName in columns)
                {
                    recordsSource[recordIndex].Add(keyName, reader[keyName].ToString());
                }
                j++;
            }
            reader.Close();
            readerSource.Close();

            foreach (var record in recordsSource)
            {
                string setScripts = string.Empty;
                string insertKeysScripts = string.Empty;
                string insertValuesScripts = string.Empty;
                int setScriptsIndex = 0;
                int insertScriptsIndex = 0;
                string primaryKeyValue = record.Key;
                if (recordsDest.ContainsKey(primaryKeyValue))
                {
                    foreach (string keyName in columns)
                    {
                        if (!ignoreUpdateColumns.Contains(keyName))
                        {
                            if (recordsDest[primaryKeyValue][keyName] == record.Value[keyName])
                            {
                                //do nothing
                            }
                            else
                            {
                                if (setScriptsIndex == 0)
                                {
                                    setScripts += keyName + "='" + recordsSource[primaryKeyValue][keyName] + "' ";
                                }
                                else
                                {
                                    setScripts += "," + keyName + "='" + recordsSource[primaryKeyValue][keyName] + "' ";
                                }
                                setScriptsIndex++;
                            }
                        }
                    }
                }
                else
                {
                    foreach (string keyName in columns)
                    {
                        if (!ignoreInsertColumns.Contains(keyName))
                        {
                            if (insertScriptsIndex == 0)
                            {
                                insertKeysScripts += keyName;
                                insertValuesScripts += "'" + recordsSource[primaryKeyValue][keyName] + "' ";
                            }
                            else
                            {
                                insertKeysScripts += "," + keyName;
                                insertValuesScripts += ",'" + recordsSource[primaryKeyValue][keyName] + "' ";
                            }
                            insertScriptsIndex++;
                        }
                    }
                }

                //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

 

 

 

 

SQL Server数据全同步及价值分析[终结版]
分享到:
sqoop使用例子
sqoop使用例子简介: sqoop是工具,一个可以把ORACLE、MYSQL中的数据导入到HDFS、HIVE、HBASE中(反过来也可以)的工具。下面是一些使用的例子。留下做个参考: #从Mysql中抽取数据到HDFS.问题:文件太多,全他妈是小文件;目标目录如果已经存在会报错 sqoop import --connect jdbc:mysql://10.10.244.137/test --username ...
MySQL触发器 trigger之for each row
MySQL触发器 trigger之for each rowfor each row 每行受影响,触发器都执行,叫行级触发器。 oracle 触发器中分行级触发器和语句级触发器,可不写for each row,无论影响多少行都只执行一次。 mysql不支持语句触发器,所以必须写for each row; 实际操作验证: #触发器test5: DELIMITER $$ CREATE TRIGGER test5 AFTER UP...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……