/// <summary>更新SyncFile</summary> /// <param name="conn">数据库连接</param> /// <param name="syncFile">文件集信息</param> /// <returns>操作是否成功</returns> public static bool UpdateSyncFile(SQLiteConnection conn, SyncFile syncFile) { if (conn == null || syncFile == null) { return(false); } int countLinkedFile = syncFile.ListLinkedFile.Count; System.Diagnostics.Debug.Assert(countLinkedFile >= 2); try { //更新SyncFile表 List <string> sqls = new List <string>(countLinkedFile + 1); string sqlSyncFile = string.Format( "UPDATE SyncFile SET FileSetName = '{0}', LastFileHash = '{1}', LastUpdatePath = '{2}', LastUpdateDate = {3}, UpdateTimes = {4} WHERE ID = {5}", syncFile.FileSetName, syncFile.LastFileHash, syncFile.LastUpdatePath, syncFile.LastUpdateDate.ToBinary(), syncFile.UpdateTimes, syncFile.ID); sqls.Add(sqlSyncFile); //删除LinkedFile表中的过时信息 var dtLinkedFile = GetDataTable("LinkedFile", conn); List <string> listFullName = syncFile.ListLinkedFile.Select(file => file.FullName).ToList(); List <int> listDel = (from dr in dtLinkedFile.AsEnumerable() let id_SyncFile = Convert.ToInt32(dr["ID_SyncFile"]) let fullPath = dr["FilePath"] + "\\" + dr["FileName"] where id_SyncFile == syncFile.ID && !listFullName.Contains(fullPath) select Convert.ToInt32(dr["ID"])).ToList(); sqls.AddRange(listDel.Select(idDel => string.Format("DELETE FROM LinkedFile WHERE ID = {0}", idDel))); //更新LinkedFile表 for (int i = 0; i < countLinkedFile; i++) { LinkedFile linkedFile = syncFile.ListLinkedFile[i]; string sql = (from dr in dtLinkedFile.AsEnumerable() let id = Convert.ToInt32(dr["ID"]) let id_SyncFile = Convert.ToInt32(dr["ID_SyncFile"]) let fullPath = dr["FilePath"] + "\\" + dr["FileName"] where id_SyncFile == linkedFile.ID_SyncFile && fullPath.Equals(linkedFile.FullName) select string.Format( "UPDATE LinkedFile SET FileName = '{0}', IsLastNewest = {1}, FilePath = '{2}', LastSyncDate = {3} WHERE ID = {4}", linkedFile.FileName, linkedFile.IsLastNewest, linkedFile.FilePath, linkedFile.LastSyncDate.ToBinary(), id)).FirstOrDefault() ?? string.Format( "INSERT INTO LinkedFile (ID_SyncFile, FileName, IsLastNewest, FilePath, LastSyncDate) VALUES ({0}, '{1}', {2}, '{3}', {4})", linkedFile.ID_SyncFile, linkedFile.FileName, linkedFile.IsLastNewest, linkedFile.FilePath, linkedFile.LastSyncDate.ToBinary()); sqls.Add(sql); } //事务执行 return(ExecuteTransSQL(sqls, conn)); } catch (Exception ex) { LogHelper.WriteErrLog("UpdateSyncFile", ex); return(false); } finally { conn.Close(); } }
/// <summary>刷新文件集信息</summary> /// <param name="syncFile">文件集</param> /// <param name="latestPath">最新文件路径</param> /// <param name="updateTime">最近更新时间</param> /// <param name="newUpdateTimes">新增更新次数(默认值为0)</param> /// <returns>操作是否成功</returns> public static bool FreshSyncFile(ref SyncFile syncFile, string latestPath, DateTime updateTime, int newUpdateTimes = 0) { if (syncFile == null || syncFile.ListLinkedFile == null || syncFile.ListLinkedFile.Count < 2) { return(false); } if (!File.Exists(latestPath)) { return(false); } FileInfo fi = new FileInfo(latestPath); syncFile.LastFileHash = fi.GetHashCode().ToString(); syncFile.LastUpdatePath = latestPath; syncFile.LastUpdateDate = updateTime; syncFile.UpdateTimes += newUpdateTimes; int count = syncFile.ListLinkedFile.Count; for (int i = 0; i < count; i++) { LinkedFile file = syncFile.ListLinkedFile[i]; file.IsLastNewest = file.FilePath.Equals(latestPath) ? 1 : 0; file.LastSyncDate = updateTime; } return(true); }
/// <summary>添加一条SyncFile数据</summary> /// <param name="conn">数据库连接</param> /// <param name="syncFile">文件集信息</param> /// <returns>操作是否成功</returns> public static bool InsertSyncFile(SQLiteConnection conn, SyncFile syncFile) { if (conn == null || syncFile == null) { return(false); } int countLinkedFile = syncFile.ListLinkedFile.Count; System.Diagnostics.Debug.Assert(countLinkedFile >= 2); try { var dtSyncFile = GetDataTable("SyncFile", conn); if (HaveIdInDt(dtSyncFile, syncFile.ID)) { return(UpdateSyncFile(conn, syncFile)); } //添加SyncFile表数据 List <string> sqls = new List <string>(countLinkedFile + 1); int id = 1; if (dtSyncFile.Rows.Count == 0) { SetAutoIncr(conn, "SyncFile"); } else { id = GetLastIdInDt(dtSyncFile) + 1; } string sqlSyncFile = string.Format( "INSERT INTO SyncFile (ID, FileSetName, LastFileHash, LastUpdatePath, LastUpdateDate, UpdateTimes) VALUES ({0}, '{1}', '{2}', '{3}', {4}, {5})", id, syncFile.FileSetName, syncFile.LastFileHash, syncFile.LastUpdatePath, syncFile.LastUpdateDate.ToBinary(), syncFile.UpdateTimes); sqls.Add(sqlSyncFile); //添加LinkedFile表数据 for (int i = 0; i < countLinkedFile; i++) { LinkedFile linkedFile = syncFile.ListLinkedFile[i]; string sql = string.Format( "INSERT INTO LinkedFile (ID_SyncFile, FileName, IsLastNewest, FilePath, LastSyncDate) VALUES ({0}, '{1}', {2}, '{3}', {4})", id, linkedFile.FileName, linkedFile.IsLastNewest, linkedFile.FilePath, linkedFile.LastSyncDate.ToBinary()); sqls.Add(sql); } //事务执行 return(ExecuteTransSQL(sqls, conn)); } catch (Exception ex) { LogHelper.WriteErrLog("InsertSyncFile", ex); return(false); } finally { conn.Close(); } }