public override bool Backup(string dbName, string backupfile, bool packing) { try { using (SqlConnection conn = new SqlConnection(GetConnectionString(true))) { conn.Open(); string dir = string.Empty; dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Temp\"); if (!Directory.Exists(dir)) { FileDirectoryOperate.CreateDirectoryEx(dir); } else { // SQL Server backup file foreach (string s in Directory.GetFiles(dir, "*.bak")) { FileDirectoryOperate.DeleteFileWithTime(s); } } #region File list to packup string tempFile = Path.Combine(dir, dbName + ".bak"); List <string> files = new List <string>(2); files.Add(tempFile); #endregion DbCommand cmd = conn.CreateCommand(); cmd.CommandText = string.Format("backup database {0} to disk = '{1}'", dbName, tempFile); cmd.CommandTimeout = 600; cmd.ExecuteNonQuery(); if (!File.Exists(tempFile)) { return(false); } if (worker != null && worker.WorkerReportsProgress) { if (packing) { worker.ReportProgress(0, string.Format("DataBase Packing Up Files......", backupfile)); } else { worker.ReportProgress(100); } } if (!packing) { return(true); } SharpZipHelper.ZipMultiFiles(files.ToArray(), backupfile, DatabaseCommon.ZipPwd, 6, worker); foreach (string file in files) { FileDirectoryOperate.DeleteFileWithTime(file); } return(true); } } catch (Exception) { return(false); } }
protected override void Backup(BackupItemEx bi) { string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Temp\"); if (!Directory.Exists(dir)) { FileDirectoryOperate.CreateDirectoryEx(dir); } if (bi.IsFullTable) { bi.BackupFileName = Path.Combine(dir, bi.TableName + ".bak"); } else { bi.BackupFileName = Path.Combine(dir, bi.TableName + "_" + bi.GuidFieldName + ".bak"); } var columnPropertyList = DatabaseHelper.GetColumnPropertyInfo(bi.TableName, bi.ItemType); try { using (FileStream fs = new FileStream(bi.BackupFileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read)) using (TextWriter tw = new StreamWriter(fs, Encoding.Unicode)) { Type blobType = typeof(object); Type stringType = typeof(string); Type doubleType = typeof(double); int recIndex = 0; do { foreach (object backupObj in bi.BackupObjects) { int count = 0; foreach (PropertyInfo pi in columnPropertyList) { bool writeContent = true; object val = pi.GetValue(backupObj, null); if (pi.PropertyType == stringType) { if (val != null && string.IsNullOrEmpty(val as string)) { val = "\0"; } } else if (pi.PropertyType == doubleType) { val = string.Format(CultureInfo.InvariantCulture, "{0:R}", val); } else if (pi.PropertyType == blobType && val is byte[]) { byte[] array = val as byte[]; EncodingOperateHelper.ToHexString(array, tw); writeContent = false; } if (writeContent) { tw.Write(val); } if (++count < columnPropertyList.Count) { tw.Write(",\0"); } } tw.WriteLine(); } bi.RecordsNum -= bi.BackupObjects.Count; recIndex += bi.BackupObjects.Count; bi.BackupObjects.Clear(); if (bi.RecordsNum > 0 && !string.IsNullOrEmpty(bi.DumpQuery)) { bi.BackupObjects = DatabaseHelper.GetBoundedEntities(bi.DumpQuery, BackupItemEx.BatchNum, recIndex); } else { break; } } while (bi.RecordsNum > 0); } } catch (System.Exception ex) { LogHelper.Log(ex); } }