Exemple #1
0
        //<Summary>Make backup Process With SQL cmd along with the created specifice dir and named the backup file name as DB names and datetime culture.
        //</Summary>
        // <param > name-string  >>> the DB name what we backup </param>
        protected static void back_pro(string name)
        {
            Base_DL bdl = new Base_DL();
            var     con = bdl.GetConnectionString();
            var     sqlConStrBuilder = new SqlConnectionStringBuilder(con);
            var     backupFolder     = SKS_pathGet("bk_path", @"C:/SMS/BackUp/");

            using (var connection = new SqlConnection(sqlConStrBuilder.ConnectionString))
            {
                var    query  = String.Format("BACKUP DATABASE {0} TO DISK='{1}'", sqlConStrBuilder.InitialCatalog, String.Format("{0}{1}-{2}.bak", backupFolder, sqlConStrBuilder.InitialCatalog, DateTime.Now.ToString("yyyy-MM-dd"))).Replace("SMS_V1", name);
                string s      = query;
                int    start  = s.IndexOf('\'') + 1;
                int    end    = s.IndexOf('\'', start);
                string result = s.Substring(start, end - start);
                if (!Directory.Exists(backupFolder))
                {
                    DirectoryInfo di = Directory.CreateDirectory(backupFolder);
                }
                if (File.Exists(result))
                {
                    File.Delete(result);
                }

                using (var command = new SqlCommand(query, connection))
                {
                    connection.Open();
                    command.ExecuteNonQuery();
                }
            }
            DeleteFilesAfter7days(7, backupFolder);
        }
        /// <summary>
        /// Make backup Process With SQL cmd along with the created specifice dir and named the backup file name as DB names and datetime culture.
        /// </summary>
        /// <param name="name">name-string  >>> the DB name what we backup </param>
        protected static void back_pro(string name)
        {
            Base_DL bdl = new Base_DL();
            var     con = bdl.GetConnectionString();

            Console.WriteLine("Test");
            var sqlConStrBuilder = new SqlConnectionStringBuilder(con);

            Console.WriteLine(sqlConStrBuilder);
            var backupFolder   = SKS_pathGet("bk_path", @"C:/SMS/BackUp/");
            var io_time_on_off = " SET STATISTICS IO OFF SET STATISTICS TIME OFF SET STATISTICS IO ON SET STATISTICS TIME ON  " + " _ " + " " + Environment.NewLine + "SET STATISTICS IO OFF  SET STATISTICS TIME OFF";

            using (var connection = new SqlConnection(sqlConStrBuilder.ConnectionString))
            {
                var    query  = String.Format("BACKUP DATABASE {0} TO DISK='{1}'", sqlConStrBuilder.InitialCatalog, String.Format("{0}{1}-{2}.bak", backupFolder, sqlConStrBuilder.InitialCatalog, DateTime.Now.ToString("yyyy-MM-dd"))).Replace("-", "").Replace("V12", "2");
                string s      = query;
                int    start  = s.IndexOf('\'') + 1;
                int    end    = s.IndexOf('\'', start);
                string result = s.Substring(start, end - start);
                if (!Directory.Exists(backupFolder))
                {
                    DirectoryInfo di = Directory.CreateDirectory(backupFolder);
                }
                if (File.Exists(result))
                {
                    File.Delete(result);
                }

                using (var command = new SqlCommand(io_time_on_off.Replace("_", query), connection))
                {
                    connection.Open();
                    command.CommandTimeout = 60000;  // 180  seconds connections timeout putting make the excecution time out failed event was fixed.     // by Ptk 20190520
                    command.ExecuteNonQuery();
                }
            }
            DeleteFilesAfter7days(7, backupFolder);
        }