www.pudn.com > startprepare.rar > OptimizeManager.cs


using System; 
using System.IO; 
using System.Data; 
using DS.EMIS.StartPrepare.Common; 
using System.Collections; 
using System.Diagnostics; 
using System.Windows.Forms; 
namespace DS.EMIS.StartPrepare 
{ 
	///  
	/// OptimizeManager 的摘要说明。 
	///  
	public class OptimizeManager 
	{ 
		 
		//		public delegate void ImageStateChangedEventHandel(bool state); 
		//		public event ImageStateChangedEventHandel OnImageStateChangedEvent; 
 
		public CtrlResultPanel ResultView ; 
		private string optConfig = "OptimizeConfig.xml"; 
		private string optScriptField = "OPTIMIZESCRIPT"; 
		public string OptConfig 
		{ 
			get 
			{ 
				 
				return this.optConfig; 
			} 
			set 
			{ 
				this.optConfig = value; 
			} 
		} 
		public string OptScriptField 
		{ 
			get 
			{ 
				 
				return this.optScriptField; 
			} 
			set 
			{ 
				this.optScriptField = value; 
			} 
		} 
 
		public OptimizeManager() 
		{ 
			 
		} 
		 
 
		///  
		/// 获取配置信息 
		///  
		/// 配置信息DT(加入修改时间列) 
		public DataTable GetOptimizeDt(string fileName) 
		{ 
			DataTable dt = GlobalObject.Instance.LocalSession.GetConfigs(fileName,this.OptScriptField); 
			dt.Columns.Add("Select",typeof(bool)); 
			dt.Columns.Add("UpdateTime",typeof(DateTime)); 
			dt.Columns.Add("StateImage",typeof(System.Byte[])); 
 
			foreach(DataRow row in dt.Rows ) 
			{ 
				string scriptFileName = row["FILENAME"].ToString(); 
				scriptFileName = ".\\Resources\\SCRIPT\\"+scriptFileName; 
				DateTime updatTime = this.GetFileUpdateTime(scriptFileName); 
				if(updatTime != DateTime.MinValue) 
				{ 
					row["UpdateTime"] = updatTime; 
				} 
				//默认为选中 
				row["Select"] = true; 
			} 
			return dt; 
		} 
 
 
		///  
		/// 执行优化脚本 
		///  
		/// 脚本实体对角集合 
		public void ExecuteOptimizeScript(IList entityList) 
		{ 
			IList optLogList = new ArrayList(); 
			foreach(OptScriptEntity entity in entityList) 
			{ 
				//组织日志对象 
				OptLogEntity optLog = new OptLogEntity(); 
				optLog.FunName = entity.DisplayName; 
				optLog.ScriptFile = entity.ScriptFile; 
				optLog.ReleaseDate = DateTime.Now; 
				optLog.Comment = entity.DisplayName+"已执行"; 
				try 
				{			 
					optLog.Result = this.Execute(entity.ScriptFile);					 
					optLogList.Add(optLog);					 
				} 
				catch 
				{ 
					 
				} 
			} 
			//写日志 
			this.writeLog(optLogList);			 
		} 
 
		///  
		/// 读取SQL脚本文件 
		///  
		/// 脚本文件字符串 
		///  
		public string ReadSQLScript(string fileName) 
		{ 
			return GlobalObject.Instance.LocalSession.ReadSQLScript(fileName); 
		} 
 
		public void WriteSQLScript(string fileName,string sqlString) 
		{ 
			GlobalObject.Instance.LocalSession.WriteSQLScript(fileName,sqlString); 
		} 
 
		///  
		/// 用sqlplus执行脚本 
		///  
		private string Execute(string sqlFileName) 
		{ 
			try 
			{ 
				Cursor.Current = Cursors.WaitCursor; 
 
				string strResult = string.Empty; 
				string batFileName = Path.Combine(Application.StartupPath,"tempSql.bat"); 
				CreateBatFile(batFileName,sqlFileName); 
 
				Process p = new Process(); 
				p.StartInfo.FileName = batFileName; 
				p.StartInfo.UseShellExecute = false;  
				p.StartInfo.RedirectStandardInput = true;  
				p.StartInfo.RedirectStandardOutput = true;  
				p.StartInfo.RedirectStandardError = true;  
				p.StartInfo.CreateNoWindow = true;  
				p.Start(); 
 
				p.StandardInput.WriteLine("quit");  
				 
				strResult += p.StandardOutput.ReadToEnd(); 
				this.ResultView.AppendLog(strResult); 
 
				p.StandardInput.WriteLine("exit"); 
				p.WaitForExit();  
 
				strResult += p.StandardOutput.ReadToEnd(); 
				this.ResultView.AppendLog(strResult); 
				p.Close(); 
				return strResult; 
			} 
			catch(Exception ex) 
			{ 
				this.ResultView.AppendLog(ex.ToString()); 
				return ex.ToString(); 
			} 
			finally 
			{ 
				Cursor.Current = Cursors.Default; 
				//FireJobFinished(); 
			} 
		} 
 
		private void CreateBatFile(string batFileName,string sqlFileName) 
		{ 
			using(StreamWriter writer = new StreamWriter(batFileName,false,System.Text.Encoding.Default)) 
			{ 
				string str = string.Format("sqlplus {0}/{1}@{2} @{3} exit",DBAccess.UserID,DBAccess.UserPD,DBAccess.ServiceName,sqlFileName); 
				writer.WriteLine(str); 
				writer.Close(); 
			} 
		} 
 
		///  
		/// 获取文件的修改时间 
		///  
		///  
		///  
		private DateTime GetFileUpdateTime(string fileName) 
		{ 
			FileInfo fileInfo = null; 
			try 
			{ 
				fileInfo = new FileInfo(fileName); 
			} 
			catch 
			{ 
				return DateTime.MinValue; 
			}		    
				 
			if(fileInfo.CreationTime.CompareTo(fileInfo.LastWriteTime) == 1) 
			{ 
				return fileInfo.CreationTime; 
			} 
			return fileInfo.LastWriteTime; 
			 
		} 
 
 
		///  
		/// 写日志 
		///  
		///  
		private void writeLog(IList optLogList) 
		{ 
			DataSet ds = new DataSet("OptimizeLog"); 
 
			foreach(OptLogEntity optLog in optLogList) 
			{ 
				DataTable dt = new DataTable(optLog.FunName); 
				 
				dt.Columns.Add("ReleaseDate",typeof(DateTime)); 
				dt.Columns.Add("FunName",typeof(string));  
				dt.Columns.Add("ScriptFile",typeof(string)); 
				dt.Columns.Add("Comment",typeof(string)); 
				dt.Columns.Add("Result",typeof(string));				 
 
				DataRow row = dt.NewRow(); 
				row["ReleaseDate"] = optLog.ReleaseDate; 
				row["FunName"]  = optLog.FunName; 
				row["ScriptFile"] = optLog.ScriptFile; 
				row["Comment"] = optLog.Comment; 
				row["Result"] = optLog.Result; 
				dt.Rows.Add(row); 
				ds.Tables.Add(dt); 
			} 
			ds.AcceptChanges(); 
			 
			GlobalObject.Instance.LocalSession.WriteDataToDataFile("OptimizeLog.xml",ds);			 
		} 
 
 
		 
	} 
 
	///  
	/// 优化脚本实体类 
	///  
	public class OptScriptEntity 
	{ 
		private string displayName = string.Empty; 
		private string scriptFile = string.Empty; 
		private DateTime UpdateTime = DateTime.MinValue; 
		public string DisplayName  
		{ 
			get 
			{ 
				return this.displayName; 
			} 
			set 
			{ 
				this.displayName = value; 
			} 
		} 
		public string ScriptFile 
		{ 
			get 
			{ 
				return this.scriptFile; 
			} 
			set 
			{ 
				this.scriptFile = value; 
			}			 
		} 
		public DateTime updateTime 
		{ 
			get 
			{ 
				return this.updateTime; 
			} 
			set 
			{ 
				this.updateTime = value; 
			} 
		} 
	} 
 
	///  
	/// 日志实体类 
	///  
	public class OptLogEntity 
	{ 
		private string funName; 
		private string scriptFile; 
		private string result; 
		private string comment; 
		private DateTime releaseDate; 
		public string FunName 
		{ 
			get 
			{ 
				return this.funName; 
			} 
			set 
			{ 
				this.funName = value; 
			} 
		} 
		public string ScriptFile 
		{ 
			get 
			{ 
				return this.scriptFile; 
			} 
			set 
			{ 
				this.scriptFile = value; 
			} 
		} 
		public string Result 
		{ 
			get 
			{ 
				return this.result; 
			} 
			set 
			{ 
				this.result = value; 
			} 
		} 
		public string Comment 
		{ 
			get 
			{ 
				return this.comment; 
			} 
			set 
			{ 
				this.comment = value; 
			} 
		} 
		public DateTime ReleaseDate 
		{ 
			get 
			{ 
				return this.releaseDate; 
			} 
			set 
			{ 
				this.releaseDate = value; 
			} 
		} 
	} 
 
}