无网不进  
软硬件开发

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         }

 

posted on 2019-01-26 10:24  无网不进  阅读(1128)  评论(0编辑  收藏  举报