public BaseResponse <bool> BackupDB() { BaseResponse <bool> response = new BaseResponse <bool>(); try { using (SqlCommand cmdBakRst = new SqlCommand()) { SqlConnection conn = new SqlConnection(EcanSecurity.Decode(Utilitys.GetAppConfig("iCMS"))); //备份文件路径 var dir = Utilitys.GetAppConfig("BackupPath"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } //生成备份文件名 var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".bak"; var abPath = Path.Combine(dir, fileName); var cmdText = string.Format(@"backup database HanBinDB to disk='{0}'", abPath); conn.Open(); cmdBakRst.Connection = conn; cmdBakRst.CommandType = CommandType.Text; cmdBakRst.CommandText = cmdText; cmdBakRst.ExecuteNonQuery(); FileInfo fileInfo = new FileInfo(abPath); var fileSize = fileInfo.Length; #region 插入备份日志 BackupLog backupLog = new BackupLog(); backupLog.BackupDate = DateTime.Now; backupLog.BackupPath = abPath; backupLog.BackupSize = fileSize; backlogRepository.AddNew <BackupLog>(backupLog); #endregion } } catch (Exception e) { LogHelper.WriteLog(e); response.IsSuccessful = false; response.Reason = e.Message; } return(response); }
/// <summary> /// 执行事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnDo_Click(object sender, EventArgs e) { if (rtxtyuanshi.Text.Trim() == "" && rtxtjiami.Text.Trim() == "") { MessageBox.Show("请输入加密前或加密后字符串。"); return; } if (rtxtyuanshi.Text.Trim() != "") { rtxtjiami.Text = EcanSecurity.Encode(rtxtyuanshi.Text.Trim()); return; } else { rtxtyuanshi.Text = EcanSecurity.Decode(rtxtjiami.Text.Trim()); return; } }
static void RestoreDB(string dataBaseName, string path) { path = @"C:\Program Files (x86)\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA"; //连接字符串 var connectionStr = EcanSecurity.Decode(Utilitys.GetAppConfig("iCMS")); DataTable DBNameTable = new DataTable(); SqlDataAdapter Adapter = new SqlDataAdapter("select name from master..sysdatabases", connectionStr); lock (Adapter) { Adapter.Fill(DBNameTable); } foreach (DataRow row in DBNameTable.Rows) { if (row["name"].ToString() == dataBaseName) { throw new Exception("已存在对应的数据,请勿重复还原数据库!"); } } //检测真正当前bak文件真正的log mdf的名字 var strsql = " restore filelistonly from disk = '" + path + "'"; SqlDataAdapter Adapter2 = new SqlDataAdapter(strsql, connectionStr); var dt = new DataTable(); lock (Adapter2) { Adapter2.Fill(dt); } var mdf = dt.Rows[0][0].ToString(); var log = dt.Rows[1][0].ToString(); string restore = string.Format(@"restore database {0} from disk = '{1}' with REPLACE , move '{2}' to 'D:\{3}.mdf' ,move '{4}' to 'D:\{5}.ldf'", dataBaseName, path, mdf, dataBaseName, log, dataBaseName); SqlConnection conn = new SqlConnection(connectionStr); SqlCommand cmd1 = new SqlCommand(restore, conn); conn.Open(); //k cmd1.ExecuteNonQuery(); conn.Close(); //g }
public ServiceConfigForm() { InitializeComponent(); IsConnOK = false; IsSaveOK = false; ConnectionString = string.Empty; this.txtTestResult.ForeColor = Color.Red; this.lblErrorDbName.Visible = false; this.lblErrorHost.Visible = false; this.lblErrorPwd.Visible = false; this.lblErrorUserID.Visible = false; this.lblkey.Visible = false; this.lblsecret.Visible = false; //this.ControlBox = false; #region MyRegion var rootPath = System.Environment.CurrentDirectory; var appFile = rootPath + "/iCMS.Server.WindowsService.exe.config"; //appFile = "F:/App.config"; //直接配置 王颖辉 2016-12-26 try { //如果找到Web.config if (File.Exists(appFile)) { XmlDocument configDoc = new XmlDocument(); configDoc.Load(appFile); XmlNode appSettingsNode = configDoc.SelectSingleNode("configuration/appSettings"); //遍历所有子节点,找到ServiceIPAddress节点并返回 foreach (XmlNode node in appSettingsNode.ChildNodes) { if (node.NodeType != XmlNodeType.Element) { continue; } XmlElement xele = (XmlElement)node; var keyValue = xele.GetAttribute("key"); switch (keyValue) { case "iCMS": { string srcConn = EcanSecurity.Decode(xele.GetAttribute("value")); if (!string.IsNullOrEmpty(srcConn)) { var arrKeyValue = srcConn.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries); foreach (string innerArr in arrKeyValue) { var innerArrTemp = innerArr.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries); switch (innerArrTemp[0]) { case "Server": this.txtHost.Text = innerArrTemp[1]; break; case "Initial Catalog": this.txtDbName.Text = innerArrTemp[1]; break; case "User Id": this.txtUserID.Text = innerArrTemp[1]; break; case "Password": this.txtUserPwd.Text = innerArrTemp[1]; break; } } } } break; case "Secret": this.tbSecret.Text = xele.GetAttribute("value"); break; case "Key": this.tbKey.Text = xele.GetAttribute("value"); break; } } } } catch (Exception ex) { MessageBox.Show(ex.Message); } #endregion }
public iCMSDbContext() : base(EcanSecurity.Decode(Utilitys.GetAppConfig("iCMS"), EcanSecurity.GetKey(Utilitys.GetAppConfig("DBSecret")))) { Database.SetInitializer <iCMSDbContext>(null); }
public iCMSDbContext() : base(EcanSecurity.Decode(Utilitys.GetAppConfig("iCMS"))) // 做手动推送时候,为了使更改的App.Config能够立即生效,故而将 { // GetAppConfig => GetAppConfigForExe,若调试出错,请联系QXM, 2017/02/21 Database.SetInitializer <iCMSDbContext>(null); }