⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optimizemanager.cs

📁 自己编写的基本Orcale的通用的数据库初始化工具。
💻 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
{
	/// <summary>
	/// OptimizeManager 的摘要说明。
	/// </summary>
	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()
		{
			
		}
		

		/// <summary>
		/// 获取配置信息
		/// </summary>
		/// <returns>配置信息DT(加入修改时间列)</returns>
		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;
		}


		/// <summary>
		/// 执行优化脚本
		/// </summary>
		/// <param name="sqlFileNames">脚本实体对角集合</param>
		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);			
		}

		/// <summary>
		/// 读取SQL脚本文件
		/// </summary>
		/// <param name="fileName">脚本文件字符串</param>
		/// <returns></returns>
		public string ReadSQLScript(string fileName)
		{
			return GlobalObject.Instance.LocalSession.ReadSQLScript(fileName);
		}

		public void WriteSQLScript(string fileName,string sqlString)
		{
			GlobalObject.Instance.LocalSession.WriteSQLScript(fileName,sqlString);
		}

		/// <summary>
		/// 用sqlplus执行脚本
		/// </summary>
		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();
			}
		}

		/// <summary>
		/// 获取文件的修改时间
		/// </summary>
		/// <param name="fileName"></param>
		/// <returns></returns>
		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;
			
		}


		/// <summary>
		/// 写日志
		/// </summary>
		/// <param name="entity"></param>
		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);			
		}


		
	}

	/// <summary>
	/// 优化脚本实体类
	/// </summary>
	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;
			}
		}
	}

	/// <summary>
	/// 日志实体类
	/// </summary>
	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;
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -