protected void Btn_Save_Click(object sender, EventArgs e) { if (Txt_FotDate.Text == "__/__/____") { Alarm1.ShowMesseage("!تاریخ را صحیح وارد نمایید", this.Page); return; } int int_Year = int.Parse(Txt_FotDate.Text.Substring(0, 4)); int int_Mounth = int.Parse(Txt_FotDate.Text.Substring(5, 2)); int int_Day = int.Parse(Txt_FotDate.Text.Substring(8, 2)); if (int_Year < 1300 || int_Year > 1400 || int_Mounth == 00 || int_Mounth > 12 || int_Day == 00 || int_Day > 31) { Alarm1.ShowMesseage("!تاریخ را صحیح وارد نمایید", this.Page); return; } if (Lts_Inherited == null) { Lts_Inherited = new Lts_InheritedDataContext(); } string Str_Msg = ""; if (Hfld_Command.Value == "Save") { string class1 = "0"; Tb_Dead1 = Lts_Inherited.Tb_Deads.SingleOrDefault(n => n.xDedNationalCode == TxtNationalcode.Text.Trim() && n.xDedIsDeleted_ == false); if (Tb_Dead1 != null) { Alarm1.ShowMesseage("!مشخصات متوفی قبلا ثبت گردیده است", this.Page); return; } Tb_Dead1 = new Tb_Dead(); Tb_Dead1.xDedFName = Txt_FirstName.Text.Trim(); Tb_Dead1.xDedLName = Txt_LastName.Text.Trim(); Tb_Dead1.xDedFatherName = Txt_FatherName.Text.Trim(); Tb_Dead1.xDedDeadDate = Txt_FotDate.Text.Trim(); //Ddl_FYear.Text.Trim() + "/" + Ddl_FMounth.Text.Trim() + "/" + Ddl_Fday.Text.Trim(); Tb_Dead1.xDedDeadPlace = Txt_DeadPlace.Text.Trim(); Tb_Dead1.xDedIdNo = Txt_IDNo.Text.Trim(); Tb_Dead1.xDedIssuancePlace = Txt_SodoorPlace.Text.Trim(); Tb_Dead1.xDedNationalCode = TxtNationalcode.Text.Trim(); Tb_Dead1.xUserId_fk = Tb_User1.xUserId_pk; Tb_Dead1.xDedRegDate = Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now.Date).ToString(); //Tb_Dead1.xDedCodeHoviat = Txt_CodeHoviati.Text.Trim().Trim(); if (Txt_CodAtba.Text.Trim() != "") { Tb_Dead1.xDedCodeAtba = Txt_CodAtba.Text.Trim(); } if (Rbtn_DSex.SelectedValue == "male") { Tb_Dead1.xDedSex = "male"; } else { Tb_Dead1.xDedSex = "fmale"; } Lts_Inherited.Tb_Deads.InsertOnSubmit(Tb_Dead1); //List<int> Lst_Class = null; Lst_Tb_File = Lts_Inherited.Tb_Files.Where(n => n.xHozeh == Tb_User1.xUser_Hozeh && n.Tb_Dead.xDedIsDeleted_ == false).OrderBy(n => Convert.ToInt32(n.xClass)).ToList(); if (Lst_Tb_File.Count != 0) { class1 = Lst_Tb_File.LastOrDefault().xClass; } Tb_File1 = new Tb_File(); Tb_File1.xClass = (int.Parse(class1) + 1).ToString(); Tb_File1.Tb_Dead = Tb_Dead1; Tb_File1.xHozeh = Txt_Hozeh.Text; Lts_Inherited.Tb_Files.InsertOnSubmit(Tb_File1); Str_Msg = "!عملیات ذخیره با موفقیت انجام شد"; Txt_Klasse.Text = (class1 + 1).ToString(); Session["Classe"] = Txt_Klasse.Text.Trim(); } else if (Hfld_Command.Value == "Edit") { if (Lts_Inherited.Tb_Deads.SingleOrDefault(n => n.xDedNationalCode == TxtNationalcode.Text.Trim()) != null && Tb_Dead1.xDedNationalCode != TxtNationalcode.Text.Trim()) { Alarm1.ShowMesseage("!مشخصات متوفی قبلا ثبت گردیده است", this.Page); return; } Tb_Dead1.xDedFName = Txt_FirstName.Text.Trim(); Tb_Dead1.xDedLName = Txt_LastName.Text.Trim(); Tb_Dead1.xDedFatherName = Txt_FatherName.Text.Trim(); Tb_Dead1.xDedDeadDate = Txt_FotDate.Text.Trim(); //Ddl_FYear.Text.Trim() + "/" + Ddl_FMounth.Text.Trim() + "/" + Ddl_Fday.Text.Trim(); Tb_Dead1.xDedDeadPlace = Txt_DeadPlace.Text.Trim(); Tb_Dead1.xDedIdNo = Txt_IDNo.Text.Trim(); Tb_Dead1.xDedIssuancePlace = Txt_SodoorPlace.Text.Trim(); Tb_Dead1.xDedNationalCode = TxtNationalcode.Text.Trim(); Tb_Dead1.xUserId_fk = Tb_User1.xUserId_pk; Tb_Dead1.xDedRegDate = Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now.Date).ToString(); if (Rbtn_DSex.SelectedIndex == 0) { Tb_Dead1.xDedSex = "male"; } else { Tb_Dead1.xDedSex = "fmale"; } Str_Msg = "!عملیات ویرایش با موفقیت انجام شد"; } try { Lts_Inherited.SubmitChanges(); Alarm1.ShowMesseage(Str_Msg, this.Page); GetDead(Tb_Dead1.xDedId_pk); ResetControls(); } catch { Alarm1.ShowMesseage("!خطا", this.Page); } }
public void Connect(Bkp_Tb_Server item) { IntPtr admin_token = default(IntPtr); WindowsIdentity wid_current = WindowsIdentity.GetCurrent(); WindowsIdentity wid_admin = null; WindowsImpersonationContext wic = null; ResutltMessage(item.xServerIP.ToString() + "|" + "در حال برقراری ارتباط..."); string Str_Directory = ""; string Str_FolderName = ""; try { if (LogonUser(item.xServerAdminName, item.xServerDomainName, item.xServerAdminPassword, 9, 0, ref admin_token) != 0) { wid_admin = new WindowsIdentity(admin_token); wic = wid_admin.Impersonate(); string Str_Domain = item.xServerDomainName.Split('.')[0]; string Str_Drive = item.xServerDriveToBackup.Trim(); Str_FolderName = Str_Domain + "_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2); Str_Directory = "\\\\" + item.xServerIP + "\\" + Str_Drive + "$\\Backup\\"; //Create Directory For Backup try { //if (System.IO.Directory.Exists(Str_Directory + Str_FolderName)) //{ // string[] Str_Files = System.IO.Directory.GetFiles(Str_Directory + Str_FolderName); // foreach (string file in Str_Files) // System.IO.File.Delete(file); //} //else //{ // System.IO.Directory.CreateDirectory(Str_Directory + Str_FolderName); //} //Create Or Open Txt File //StreamWriter StreamWriter1 = new StreamWriter(Str_Directory + "AutoBackup_AddressFileToZip.txt"); //StreamWriter1.Write(Str_FolderName); //StreamWriter1.Close(); //Copy Consol Zip And DLL if (item.xServerDriveToBackup.Trim() == "C") { if (!File.Exists(Str_Directory + "Console_ZipOnC.exe")) { System.IO.File.Copy("E:\\Sources\\Console_ZipOnC.exe", Str_Directory + "Console_ZipOnC.exe", true); } } else if (item.xServerDriveToBackup.Trim() == "D") { if (!File.Exists(Str_Directory + "Console_ZipOnD.exe")) { System.IO.File.Copy("E:\\Sources\\Console_ZipOnD.exe", Str_Directory + "Console_ZipOnD.exe", true); } } else if (item.xServerDriveToBackup.Trim() == "E") { if (!File.Exists(Str_Directory + "Console_ZipOnE.exe")) { System.IO.File.Copy("E:\\Sources\\Console_ZipOnE.exe", Str_Directory + "Console_ZipOnE.exe", true); } } else if (item.xServerDriveToBackup.Trim() == "F") { if (!File.Exists(Str_Directory + "Console_ZipOnF.exe")) { System.IO.File.Copy("E:\\Sources\\Console_ZipOnF.exe", Str_Directory + "Console_ZipOnF.exe", true); } } if (!File.Exists(Str_Directory + "ICSharpCode.SharpZipLib.dll")) { System.IO.File.Copy("E:\\Sources\\ICSharpCode.SharpZipLib.dll", Str_Directory + "ICSharpCode.SharpZipLib.dll", true); } } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا درانتقال فایل : " + ex.ToString()); } ////Create Connection And open to backup string Str_ConnectionString = "Data Source=" + item.xServerIP + (item.xServerSQLInstance != null ? ("\\" + item.xServerSQLInstance) : "") + ";User ID=" + item.xServerSQLAdminName + ";password="******";Integrated Security=True;Connect Timeout=120"; SqlConnection Sql_conn = new SqlConnection(Str_ConnectionString); SqlCommand Sql_Comm = new SqlCommand(); Sql_Comm.Connection = Sql_conn; try { Sql_conn.Open(); ResutltMessage(item.xServerIP.ToString() + "|" + "ارتباط موفق با بانک اطلاعاتی"); } catch { ResutltMessage(item.xServerIP.ToString() + "|" + "ارتباط ناموفق با بانک اطلاعاتی"); if (!Lst_ServerError.Contains(item)) { LoadErrorServer(item); } else { LoadServer(1); return; } } //create directory string Str_CreateDirectory = " EXEC master..xp_CMDShell 'mkdir " + item.xServerDriveToBackup.Trim() + ":\\Backup\\" + Str_FolderName + "' "; Sql_Comm.CommandText = Str_CreateDirectory; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { Str_CreateDirectory = "EXEC sp_configure 'show advanced options', 1 " + " RECONFIGURE EXEC sp_configure 'xp_cmdshell', 1 " + " RECONFIGURE " + " EXEC master..xp_CMDShell 'mkdir " + item.xServerDriveToBackup.Trim() + ":\\Backup\\" + Str_FolderName + "' "; Sql_Comm.CommandText = Str_CreateDirectory; Sql_Comm.CommandTimeout = 0; Sql_Comm.ExecuteNonQuery(); ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); } //delete all files in directory string Str_DeleteDirectoryFiles = " EXEC master..xp_CMDShell 'del " + item.xServerDriveToBackup.Trim() + ":\\Backup\\" + Str_FolderName + "\\*.bak' "; Sql_Comm.CommandText = Str_DeleteDirectoryFiles; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); } Lts_BackupDataContext Lts_Backup = new Lts_BackupDataContext(); List <Bkp_Tb_DataBase> Lst_DataBases = Lts_Backup.Bkp_Tb_ServerDataBases.Where(b => b.xServerId_fk == item.xServerId_pk).Select(b => b.Bkp_Tb_DataBase).ToList(); string Str_Databases = ""; foreach (Bkp_Tb_DataBase DatabaseName in Lst_DataBases) { Str_Databases += "'" + DatabaseName.xDataBaseName + "',"; } Str_Databases = Str_Databases.Substring(0, Str_Databases.Length - 1); string Str_BackupQuery = "DECLARE @name VARCHAR(50) " + "DECLARE @path VARCHAR(256) " + "DECLARE @fileName VARCHAR(256) " + "DECLARE @fileDate VARCHAR(20) " + "SET @path ='" + item.xServerDriveToBackup.Trim() + ":\\" + "Backup\\" + Str_FolderName + "\\'" + " SELECT @fileDate = CONVERT(VARCHAR(20),GETDATE(),112) " + "DECLARE db_cursor CURSOR FOR " + "SELECT name " + "FROM master.dbo.sysdatabases " + "WHERE name IN (" + Str_Databases + ") " + "OPEN db_cursor " + "FETCH NEXT FROM db_cursor INTO @name " + "WHILE @@FETCH_STATUS = 0 " + "BEGIN " + "SET @fileName = @path + @name + '.bak' " + "BACKUP DATABASE @name TO DISK = @fileName " + "FETCH NEXT FROM db_cursor INTO @name " + "END " + "CLOSE db_cursor " + "DEALLOCATE db_cursor"; Sql_Comm.CommandText = Str_BackupQuery; Sql_Comm.CommandTimeout = 0; ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات پشتیبان گیری در : " + DateTime.Now.ToShortTimeString()); try { Sql_Comm.ExecuteNonQuery(); ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات پشتیبان گیری در : " + DateTime.Now.ToShortTimeString()); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); if (!Lst_ServerError.Contains(item)) { LoadErrorServer(item); } else { LoadServer(1); return; } } //StreamReader StreamReader1 = new StreamReader(Str_Directory + "AutoBackup_AddressFileToZip.txt"); //string Str_Msg = StreamReader1.ReadLine(); //StreamReader1.Close(); //StreamWriter StreamWriter2 = new StreamWriter(Str_Directory + "AutoBackup_AddressFileToZip.txt"); //StreamWriter2.Write(Str_Msg + "&" + "Backup Success"); //StreamWriter2.Close(); // string Str_WriteFileName = " EXEC master..xp_CMDShell 'echo " + Str_FolderName + "^&" + "Backup Success > " + item.xServerDriveToBackup.Trim() + ":\\Backup\\AutoBackup_AddressFileToZip.txt' "; Sql_Comm.CommandText = Str_WriteFileName; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); } // ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات فشرده سازی در : " + DateTime.Now.ToShortTimeString()); Str_BackupQuery = " EXEC master..xp_CMDShell '" + item.xServerDriveToBackup.Trim() + ":\\Backup\\Console_ZipOn" + item.xServerDriveToBackup.Trim() + ".exe' "; Sql_Comm.CommandText = Str_BackupQuery; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); } ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات فشرده سازی در : " + DateTime.Now.ToShortTimeString()); //del files string Str_DeleteDirectoryFiles1 = " EXEC master..xp_CMDShell 'del " + item.xServerDriveToBackup.Trim() + ":\\Backup\\" + Str_FolderName + "\\*.bak' "; Sql_Comm.CommandText = Str_DeleteDirectoryFiles1; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); return; } //del dir string Str_DeleteDirectory = " EXEC master..xp_CMDShell 'RMDIR " + item.xServerDriveToBackup.Trim() + ":\\Backup\\" + Str_FolderName + "' "; Sql_Comm.CommandText = Str_DeleteDirectory; Sql_Comm.CommandTimeout = 0; try { Sql_Comm.ExecuteNonQuery(); } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); } Sql_conn.Close(); // ResutltMessage(item.xServerIP.ToString() + "|" + "شروع عملیات کپی در : " + DateTime.Now.ToShortTimeString()); if (!Directory.Exists("G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2))) { Directory.CreateDirectory("G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2)); } try { File.Copy(Str_Directory + Str_FolderName + ".zip", "G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2) + "\\" + Str_FolderName + ".zip", true); ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات کپی در : " + DateTime.Now.ToShortTimeString()); try { File.Delete(Str_Directory + Str_FolderName + ".zip"); } catch { } } catch { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا در عملیات کپی : "); ResutltMessage(item.xServerIP.ToString() + "|" + "!تلاش مجدد برای عملیات کپی : "); try { File.Copy(Str_Directory + Str_FolderName + ".zip", "G:\\Bkp_" + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(5, 2) + Class_ShamsiDateTime.MilladiToShamsi(DateTime.Now).ToString().Substring(8, 2) + "\\" + Str_FolderName + ".zip", true); ResutltMessage(item.xServerIP.ToString() + "|" + "اتمام عملیات کپی در : " + DateTime.Now.ToShortTimeString()); try { File.Delete(Str_Directory + Str_FolderName + ".zip"); } catch { } } catch { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا مجدد در عملیات کپی : "); } } LoadServer(1); } //} //catch (Exception ex) //{ // ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); // //LoadServer(1); // //if (Directory.Exists(Str_Directory + Str_FolderName)) // // try // // { // // Directory.Delete(Str_Directory + Str_FolderName, true); // // } // // catch { } // //if (File.Exists(Str_Directory + Str_FolderName + ".zip")) // // try // // { // // File.Delete(Str_Directory + Str_FolderName + ".zip"); // // } // // catch { } // if (!Lst_ServerError.Contains(item)) // { // LoadErrorServer(item); // } //} } catch (Exception ex) { ResutltMessage(item.xServerIP.ToString() + "|" + "!خطا : " + ex.ToString()); LoadServer(1); } }