Example #1
0
        /// <summary>
        /// 备份数据库
        /// </summary>
        public bool BackupDataBase(string strDbName, string strFileName, ProgressBar proBar)
        {
            SQLDMO.SQLServer sqlServer = new SQLDMO.SQLServer();
            try
            {
                string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["PSAP.Properties.Settings.PSAPConnectionString"].ConnectionString;
                string tmpStr           = "Data Source=";
                string DataSource       = connectionString.Substring(connectionString.IndexOf(tmpStr) + tmpStr.Length);
                DataSource = DataSource.Substring(0, DataSource.IndexOf(";"));

                tmpStr = "User ID=";
                string UserID = connectionString.Substring(connectionString.IndexOf(tmpStr) + tmpStr.Length);
                if (UserID.IndexOf(";") == -1)
                {
                    UserID = UserID.Substring(0);
                }
                else
                {
                    UserID = UserID.Substring(0, UserID.IndexOf(";"));
                }

                tmpStr = "Password="******";") == -1)
                {
                    Password = Password.Substring(0);
                }
                else
                {
                    Password = Password.Substring(0, Password.IndexOf(";"));
                }

                sqlServer.Connect(DataSource, UserID, Password);
                SQLDMO.Backup backup = new SQLDMO.Backup();
                proBar.Value                       = 0;
                backup.Action                      = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                backup.Initialize                  = true;
                backup.PercentComplete            += new SQLDMO.BackupSink_PercentCompleteEventHandler(Backup_PercentComplete);
                backup.Files                       = strFileName;
                backup.Database                    = strDbName;
                backup.PercentCompleteNotification = 1;
                backup.SQLBackup(sqlServer);
                proBar.Value            = 100;
                backup.PercentComplete -= new SQLDMO.BackupSink_PercentCompleteEventHandler(Backup_PercentComplete);
                backup = null;
                return(true);
            }
            catch (Exception err)
            {
                throw (new Exception("备份数据库失败" + err.Message));
            }
            finally
            {
                sqlServer.DisConnect();
            }
        }
Example #2
0
 public static void CancelDbBackup(SQLDMO.Backup oBackup, SQLDMO.BackupSink_PercentCompleteEventHandler Backup_PercentComplete)
 {
     try
     {
         oBackup.Abort();
         oBackup.PercentComplete -= Backup_PercentComplete;
     }
     catch (Exception e)
     {
         TopFashion.WriteLog.CreateLog("数据库操作", "CancelDbBackup", "error", "取消数据库备份失败:" + e.Message);
     }
 }
Example #3
0
 public static void CancelDbBackup(SQLDMO.Backup oBackup, SQLDMO.BackupSink_PercentCompleteEventHandler Backup_PercentComplete)
 {
     try
     {
         oBackup.Abort();
         oBackup.PercentComplete -= Backup_PercentComplete;
     }
     catch (Exception e)
     {
         MessageBox.Show("取消数据库备份失败:" + e.Message);
     }
 }
Example #4
0
        /// <summary>
        /// SQL数据库备份
        /// </summary>
        /// <param name="path">备份到的路径< /param>
        /// <param name="Backup_PercentComplete">进度</param>
        /// <param name="oBackup">数据库备份服务对象</param>
        /// <param name="remark">备份备注</param>
        public static bool SQLDbBackup(string path, SQLDMO.BackupSink_PercentCompleteEventHandler Backup_PercentComplete, out SQLDMO.Backup oBackup, string remark = null)
        {
            SqlConnectionStringBuilder connStrBuilder = new SqlConnectionStringBuilder(TopFashion.Configs.ConnString);
            string ServerIP      = connStrBuilder.DataSource;
            string LoginUserName = connStrBuilder.UserID;
            string LoginPass     = connStrBuilder.Password;
            string DBName        = connStrBuilder.InitialCatalog;
            string dir           = path + "\\" + DBName;

            dir = dir.Replace("\\\\", "\\");

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            string locale     = string.Empty;
            string username   = string.Empty;
            string password   = string.Empty;
            string authority  = string.Empty;
            string RemoteAuth = TopFashion.Configs.RemoteAuth;

            if (!string.IsNullOrEmpty(RemoteAuth))
            {
                string[] ra = RemoteAuth.Split("|".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                if (ra.Length == 4)
                {
                    locale    = ra[0];
                    username  = ra[1];
                    password  = ra[2];
                    authority = ra[3];
                }
            }
            System.Management.ManagementScope scope = new System.Management.ManagementScope("\\\\" + ServerIP + "\\root\\cimv2", new System.Management.ConnectionOptions(locale, username, password, authority, System.Management.ImpersonationLevel.Impersonate, System.Management.AuthenticationLevel.Default, true, null, TimeSpan.MaxValue));
            if (ServerIP == "." || ServerIP == "(local)" || ServerIP == "127.0.0.1")
            {
            }
            else
            {
                int i = WmiShareRemote.CreateRemoteDirectory(scope, Path.GetDirectoryName(dir), Directory.GetParent(dir).FullName);
            }
            string DBFile   = DBName + DateTime.Now.ToString("yyyyMMddHHmm");
            string filename = dir + "\\" + DBFile + ".bak";

            if (!File.Exists(filename))
            {
                try
                {
                    FileStream fs = File.Create(filename);
                    fs.Close();
                }
                catch (Exception e)
                {
                    TopFashion.WriteLog.CreateLog("数据库操作", "SQLDbBackup", "error", "无法创建 [" + filename + "] 数据库备份文件!" + Environment.NewLine + e.Message);
                }
            }
            if (ServerIP == "." || ServerIP == "(local)" || ServerIP == "127.0.0.1")
            {
            }
            else
            {
                bool flag = WmiShareRemote.WmiFileCopyToRemote(ServerIP, username, password, dir, "DatabaseBackup", "数据库备份集", null, new string[] { filename }, 0);
            }
            oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginUserName, LoginPass);
                oBackup.Action               = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.PercentComplete     += Backup_PercentComplete;
                oBackup.Database             = DBName;
                oBackup.Files                = @"" + string.Format("[{0}]", filename) + "";
                oBackup.BackupSetName        = DBName;
                oBackup.BackupSetDescription = "备份集" + DBName;
                oBackup.Initialize           = true;
                oBackup.SQLBackup(oSQLServer);//这里可能存在问题:比如备份远程数据库的时候,选的路径path却是本地的,恰好是远程服务器上不存在的目录
                TopFashion.SQLDBHelper SqlHelper = new TopFashion.SQLDBHelper();
                SqlHelper.ExecuteSql("insert into Record (DB, Path, Remark) values ('" + DBName + "', '" + filename + "', '" + remark + "')", true);
                return(true);
            }
            catch (Exception e)
            {
                TopFashion.WriteLog.CreateLog("数据库操作", "SQLDbBackup", "error", "备份时出错:" + e.Message);
            }
            finally
            {
                oSQLServer.DisConnect();
            }
            return(false);
        }
Example #5
0
        /// <summary>
        /// SQL数据库备份
        /// </summary>
        /// <param name="filename">要备份到的文件全路径</param>
        /// <param name="connStrBuilder">连接字符串构造器</param>
        /// <param name="Backup_PercentComplete">进度</param>
        /// <param name="oBackup">数据库备份服务对象</param>
        /// <param name="remark">备份备注</param>
        public bool SQLDbBackup(string filename, SqlConnectionStringBuilder connStrBuilder, SQLDMO.BackupSink_PercentCompleteEventHandler Backup_PercentComplete, out SQLDMO.Backup oBackup, string remark)
        {
            string   ServerIP      = connStrBuilder.DataSource;
            string   LoginUserName = connStrBuilder.UserID;
            string   LoginPass     = connStrBuilder.Password;
            string   DBName        = connStrBuilder.InitialCatalog;
            FileInfo fi            = new FileInfo(filename);
            string   DBFile        = fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length);

            oBackup = new SQLDMO.BackupClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect(ServerIP, LoginUserName, LoginPass);
                oBackup.Action               = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
                oBackup.PercentComplete     += Backup_PercentComplete;
                oBackup.Database             = DBName;
                oBackup.Files                = @"" + string.Format("[{0}]", filename) + "";
                oBackup.BackupSetName        = DBFile;
                oBackup.BackupSetDescription = "备份集" + DBFile;
                oBackup.Initialize           = true;
                oBackup.SQLBackup(oSQLServer);
                return(true);
            }
            catch (Exception e)
            {
                MessageBox.Show("数据库备份失败:" + e.Message);
                return(false);
            }
            finally
            {
                oSQLServer.DisConnect();
            }
        }