Esempio n. 1
0
 private void InitialCategory_Enter(object sender, EventArgs e)
 {
     try
     {
         InitialCategory.Items.Clear();
         var lst = CLSServerDBs.GetAll(TextBox1.Text);
         if (lst != null && lst.Count > 0)
         {
             foreach (var item in lst)
             {
                 if (item.Name.ToLower() != "master" &&
                     item.Name.ToLower() != "model" &&
                     item.Name.ToLower() != "msdb" &&
                     item.Name.ToLower() != "tempdb")
                 {
                     InitialCategory.Items.Add(item.Name);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         WebErrorLog.ErrorInstence.StartErrorLog(ex);
     }
 }
Esempio n. 2
0
        public static async Task <ReturnedSaveFuncInfo> ReStoreDbAsync(string connectionString, string pathf, bool autoBackup, ENSource Source)
        {
            var ret = new ReturnedSaveFuncInfo();
            var cn  = new SqlConnection();

            try
            {
                GC.Collect();
                SqlConnection.ClearAllPools();

                if (autoBackup)
                {
                    var dir = Application.StartupPath + "\\NovinPardazBackUp";
                    if (!Directory.Exists(dir))
                    {
                        Directory.CreateDirectory(dir);
                    }

                    var file = Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly()?.Location)?.Replace(".exe", "__");
                    var d    = Calendar.MiladiToShamsi(DateTime.Now).Replace("/", "_");
                    d    += "__" + DateTime.Now.Hour + " - " + DateTime.Now.Minute;
                    file += d;

                    var filepath = dir + "\\" + file + ".Bak";
                    ret.AddReturnedValue(await BackupDbAsync(connectionString, Source, filepath, Guid.NewGuid()));
                }

                if (pathf.EndsWith(".NPZ") || pathf.EndsWith(".npz"))
                {
                    var zp = new Zip();
                    pathf = zp.ExtractTempDIR(pathf);
                }
                else if (pathf.EndsWith(".NPZ2") || pathf.EndsWith(".npz2"))
                {
                    try
                    {
                        var zp  = new CompressFile();
                        var res = await zp.ExtractTempDIR(pathf);

                        ret.AddReturnedValue(res);
                        if (res.HasError)
                        {
                            return(ret);
                        }
                        pathf = res.value;
                    }
                    catch
                    {
                        var zp = new Zip();
                        pathf = zp.ExtractTempDIR(pathf);
                    }
                }

                var csb = new SqlConnectionStringBuilder(connectionString);
                var serverConnectionString =
                    connectionString.Replace("Initial Catalog=" + csb.InitialCatalog, "Initial Catalog=");
                cn = new SqlConnection(serverConnectionString)
                {
                    ConnectionString = serverConnectionString
                };
                var cmd        = new SqlCommand("RESTORE DATABASE [dbName] FROM  DISK = N'bkPath' WITH  FILE = 1,  MOVE N'novin' TO N'dbMDFPath',  MOVE N'novin_log' TO N'dbLDFPath',   NOUNLOAD,  REPLACE,  STATS = 5", cn);
                var dbInfo     = new CLSServerDBs(connectionString);
                var backUpInfo = new DataBaseBackUpInfo(pathf, cn.ConnectionString);

                cmd.CommandText = cmd.CommandText.Replace("dbName", csb.InitialCatalog);
                cmd.CommandText = cmd.CommandText.Replace("bkPath", pathf);
                cmd.CommandText = cmd.CommandText.Replace("novin", backUpInfo.LogicalName);
                cmd.CommandText = cmd.CommandText.Replace("novin_log", backUpInfo.LogicalName + "_log.ldf");
                cmd.CommandText = cmd.CommandText.Replace("dbMDFPath", dbInfo.FileName);
                cmd.CommandText =
                    cmd.CommandText.Replace("dbLDFPath", dbInfo.FileName.ToLower().Replace(".mdf", "_log.Ldf"));

                //تایم اوت بالا بردن مجاز است
                cmd.CommandTimeout = 5 * 60 * 1000;//5 minutes

                cn.Open();
                var temp = cmd.CommandText;
                cmd.CommandText = "ALTER DATABASE [" + csb.InitialCatalog + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE";
                cmd.ExecuteNonQuery();

                cmd.CommandText = temp;
                cmd.ExecuteNonQuery();

                // cmd.CommandText = NovinPardaz.Properties.Resources.DatabaseSettings;
                cmd.CommandText = cmd.CommandText.Replace("dbName", csb.InitialCatalog);
                cmd.ExecuteNonQuery();

                cmd.CommandText = "ALTER DATABASE [" + csb.InitialCatalog + "] SET Multi_USER";
                cmd.ExecuteNonQuery();
                cn.Close();

                SqlConnection.ClearAllPools();
                Application.DoEvents();
            }
            catch (SqlException ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                try { cn.Close(); } catch { }
                ret.AddReturnedValue(ReturnedState.Error, $"حساب کاربری SQL دسترسی به مسیر نصب برنامه ندارد\r\n{ex.Message }//{ex?.InnerException?.Message ?? ""}");
            }
            catch (Exception ex)
            {
                WebErrorLog.ErrorInstence.StartErrorLog(ex);
                try { cn.Close(); } catch { }
                ret.AddReturnedValue(ex);
            }
            Task.Run(() => SetMultiUser(connectionString));
            return(ret);
        }