C#數據同步中基本步驟和用到的相關函數

C#數據同步中基本步驟和用到的相關函數

數據同步對比步驟:html

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         }
相關文章
相關標籤/搜索