• C#数据同步中基本步骤和用到的相关函数


    C#数据同步中基本步骤和用到的相关函数

    数据同步对比步骤:

    1.将两数据库中对应的数据表分别生成XML文件

     1          /// <summary>
     2         /// 将一个DataTable以xml方式存入指定的文件中
     3         /// </summary>
     4         /// <param name="dt"></param>
     5         /// <param name="filePath"></param>
     6         public void SaveDataTableToXml(DataTable dt, string filePath)
     7         {
     8             //创建文件夹
     9             if (!Directory.Exists(Path.GetDirectoryName(filePath)))
    10             {
    11                 Directory.CreateDirectory(Path.GetDirectoryName(filePath));
    12             }
    13 
    14             DataSet ds = new DataSet();
    15             ds.Tables.Add(dt.Copy());
    16             ds.WriteXml(filePath);
    17         }
    18 
    19         /// <summary>
    20         /// 从一个指定的文件中读取DataTable
    21         /// </summary>
    22         /// <param name="filePath"></param>
    23         public DataTable ReadDataTableFromXml(string filePath)
    24         {
    25             DataSet ds = new DataSet();
    26             ds.ReadXml(filePath);
    27             if (ds.Tables.Count > 0)
    28             {
    29                 return ds.Tables[0];
    30             }
    31             else
    32             {
    33                 return null;
    34             }
    35         }

    2.上传要对比的XML数据文件到服务器端或者是从服务器下载XML文件到本地

            C#Sockect异步传送或者WebClient方式传送

     

    3.对比要同步的数据资料

     1          /// <summary>
     2         /// 对比文件
     3         /// </summary>
     4         /// <param name="localFile">本地文件</param>
     5         /// <param name="remoteFile">远程文件</param>
     6         /// <returns></returns>
     7         private bool FileCompare(string localFile, string remoteFile)
     8         {
     9             int localFilebyte;
    10             int remoteFilebyte;
    11             FileStream localFileStream;
    12             FileStream remoteFileStream;
    13             if (localFile == remoteFile)
    14             {
    15                 return true;
    16             }
    17             localFileStream = new FileStream(localFile, FileMode.Open);
    18             remoteFileStream = new FileStream(remoteFile, FileMode.Open);
    19             if (localFileStream.Length != remoteFileStream.Length)
    20             {
    21                 localFileStream.Close();
    22                 remoteFileStream.Close();
    23                 return false;
    24             }
    25             do
    26             {
    27                 localFilebyte = localFileStream.ReadByte();
    28                 remoteFilebyte = remoteFileStream.ReadByte();
    29             }
    30             while ((localFilebyte == remoteFilebyte) && (localFilebyte != -1));
    31             localFileStream.Close();
    32             remoteFileStream.Close();
    33             return ((localFilebyte - remoteFilebyte) == 0);
    34         }
    35         /// <summary>
    36         /// 对比数据表
    37         /// </summary>
    38         /// <param name="localDataTable">本地数据表</param>
    39         /// <param name="remoteDataTable">远程数据表</param>
    40         /// <returns></returns>
    41         public bool DataTableCompare(DataTable localDataTable, DataTable remoteDataTable)
    42         {
    43             if (localDataTable == null || remoteDataTable == null)
    44             {
    45                 return false;
    46             }
    47             if (localDataTable.Rows.Count != remoteDataTable.Rows.Count)
    48             {
    49                 return false;
    50             }
    51             if (localDataTable.Columns.Count != remoteDataTable.Columns.Count)
    52             {
    53                 return false;
    54             }
    55             for (int i = 0; i < localDataTable.Rows.Count; i++)
    56             {
    57                 for (int j = 0; j < localDataTable.Columns.Count; j++)
    58                 {
    59                     if (localDataTable.Rows[i][j].ToString() != remoteDataTable.Rows[i][j].ToString())
    60                     {
    61                         return false;
    62                     }
    63                 }
    64             }
    65             return true;
    66         }
  • 相关阅读:
    bow lsa plsa
    拉普拉斯平滑处理 Laplace Smoothing
    程序员的出路在哪里
    Android各代码层获取系统时间的方法
    TCP/IP笔记 二.网络层(2)——ICMP,RIP,OSPF,BGP
    利用Xtrabackup备份集合恢复一台从库的过程
    char *和char[]的区别,困扰很长时间了,总结下
    不使用webview,用手机浏览器的android app
    [置顶] oracle 快速查询数据库各种信息、及转换对应java代码
    MySQL备份方案-->(利用mysqldump以及binlog二进制日志)
  • 原文地址:https://www.cnblogs.com/hbtmwangjin/p/10322724.html
Copyright © 2020-2023  润新知