C#数据同步中基本步骤和用到的相关函数
2010-03-17 14:30
351 查看
数据同步对比步骤:
1.将两数据库中对应的数据表分别生成XML文件
/// <summary>
/// 将一个DataTable以xml方式存入指定的文件中
/// </summary>
/// <param name="dt"></param>
/// <param name="filePath"></param>
public void SaveDataTableToXml(DataTable dt, string filePath)
{
//创建文件夹
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
DataSet ds = new DataSet();
ds.Tables.Add(dt.Copy());
ds.WriteXml(filePath);
}
/// <summary>
/// 从一个指定的文件中读取DataTable
/// </summary>
/// <param name="filePath"></param>
public DataTable ReadDataTableFromXml(string filePath)
{
DataSet ds = new DataSet();
ds.ReadXml(filePath);
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
2.上传要对比的XML数据文件到服务器端或者是从服务器下载XML文件到本地
C#Sockect异步传送或者WebClient方式传送
3.对比要同步的数据资料
/// <summary>
/// 对比文件
/// </summary>
/// <param name="localFile">本地文件</param>
/// <param name="remoteFile">远程文件</param>
/// <returns></returns>
private bool FileCompare(string localFile, string remoteFile)
{
int localFilebyte;
int remoteFilebyte;
FileStream localFileStream;
FileStream remoteFileStream;
if (localFile == remoteFile)
{
return true;
}
localFileStream = new FileStream(localFile, FileMode.Open);
remoteFileStream = new FileStream(remoteFile, FileMode.Open);
if (localFileStream.Length != remoteFileStream.Length)
{
localFileStream.Close();
remoteFileStream.Close();
return false;
}
do
{
localFilebyte = localFileStream.ReadByte();
remoteFilebyte = remoteFileStream.ReadByte();
}
while ((localFilebyte == remoteFilebyte) && (localFilebyte != -1));
localFileStream.Close();
remoteFileStream.Close();
return ((localFilebyte - remoteFilebyte) == 0);
}
/// <summary>
/// 对比数据表
/// </summary>
/// <param name="localDataTable">本地数据表</param>
/// <param name="remoteDataTable">远程数据表</param>
/// <returns></returns>
public bool DataTableCompare(DataTable localDataTable, DataTable remoteDataTable)
{
if (localDataTable == null || remoteDataTable == null)
{
return false;
}
if (localDataTable.Rows.Count != remoteDataTable.Rows.Count)
{
return false;
}
if (localDataTable.Columns.Count != remoteDataTable.Columns.Count)
{
return false;
}
for (int i = 0; i < localDataTable.Rows.Count; i++)
{
for (int j = 0; j < localDataTable.Columns.Count; j++)
{
if (localDataTable.Rows[i][j].ToString() != remoteDataTable.Rows[i][j].ToString())
{
return false;
}
}
}
return true;
}
1.将两数据库中对应的数据表分别生成XML文件
/// <summary>
/// 将一个DataTable以xml方式存入指定的文件中
/// </summary>
/// <param name="dt"></param>
/// <param name="filePath"></param>
public void SaveDataTableToXml(DataTable dt, string filePath)
{
//创建文件夹
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
{
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
}
DataSet ds = new DataSet();
ds.Tables.Add(dt.Copy());
ds.WriteXml(filePath);
}
/// <summary>
/// 从一个指定的文件中读取DataTable
/// </summary>
/// <param name="filePath"></param>
public DataTable ReadDataTableFromXml(string filePath)
{
DataSet ds = new DataSet();
ds.ReadXml(filePath);
if (ds.Tables.Count > 0)
{
return ds.Tables[0];
}
else
{
return null;
}
}
2.上传要对比的XML数据文件到服务器端或者是从服务器下载XML文件到本地
C#Sockect异步传送或者WebClient方式传送
3.对比要同步的数据资料
/// <summary>
/// 对比文件
/// </summary>
/// <param name="localFile">本地文件</param>
/// <param name="remoteFile">远程文件</param>
/// <returns></returns>
private bool FileCompare(string localFile, string remoteFile)
{
int localFilebyte;
int remoteFilebyte;
FileStream localFileStream;
FileStream remoteFileStream;
if (localFile == remoteFile)
{
return true;
}
localFileStream = new FileStream(localFile, FileMode.Open);
remoteFileStream = new FileStream(remoteFile, FileMode.Open);
if (localFileStream.Length != remoteFileStream.Length)
{
localFileStream.Close();
remoteFileStream.Close();
return false;
}
do
{
localFilebyte = localFileStream.ReadByte();
remoteFilebyte = remoteFileStream.ReadByte();
}
while ((localFilebyte == remoteFilebyte) && (localFilebyte != -1));
localFileStream.Close();
remoteFileStream.Close();
return ((localFilebyte - remoteFilebyte) == 0);
}
/// <summary>
/// 对比数据表
/// </summary>
/// <param name="localDataTable">本地数据表</param>
/// <param name="remoteDataTable">远程数据表</param>
/// <returns></returns>
public bool DataTableCompare(DataTable localDataTable, DataTable remoteDataTable)
{
if (localDataTable == null || remoteDataTable == null)
{
return false;
}
if (localDataTable.Rows.Count != remoteDataTable.Rows.Count)
{
return false;
}
if (localDataTable.Columns.Count != remoteDataTable.Columns.Count)
{
return false;
}
for (int i = 0; i < localDataTable.Rows.Count; i++)
{
for (int j = 0; j < localDataTable.Columns.Count; j++)
{
if (localDataTable.Rows[i][j].ToString() != remoteDataTable.Rows[i][j].ToString())
{
return false;
}
}
}
return true;
}
相关文章推荐
- C#数据同步中基本步骤和用到的相关函数
- C#数据同步中基本步骤和用到的相关函数
- C#数据同步中基本步骤和用到的相关函数
- OkHttp 的基本数据请求 步骤 模板 (同时包裹异步请求和同步请求)
- 讲Linux下面线程的操作、多线程的同步和互斥相关函数及操作的文章链接
- ANSI、unicode、utf-8、DBCS等字符集及相关数据类型、函数的区别
- MySQL数据同步配置步骤
- 用Windows API进行串口编程的一般步骤及相关函数讲解
- 数据类型(基本内置类型)——指针和指针孩子(指针变量(指针类型)、数组(构造类型)、字符串、函数指针、空指针等)
- oracle PL/SQL的基本构成,块结构和基本语法要求,数据类型,变量定义,运算符和函数
- js中和数据类型相关的函数
- 第五章 STL 基本概念、相关函数使用总结
- C语言基础学习基本数据类型-字符专属的输入输出函数
- 读javascript高级程序设计01-基本概念、数据类型、函数
- 同步项目中需要用到百万条数据分页
- 多线程基本步骤及常用函数
- 第三章(基本概念——语法,数据类型,流程控制语句,函数)
- 学习笔记(二)JavaScript基本概念(语法,数据类型,控制语句,函数)
- C语言入门-基本数据类型和输入输出函数
- mssql数据同步实现数据复制的步骤第1/2页