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


using System; 
using System.IO; 
 
namespace DS.EMIS.StartPrepare.Common 
{ 
	///  
	/// ManageSQLScripts 的摘要说明。SQL脚本读写控制类 
	///  
	internal class ManageSQLScripts 
	{ 
		public ManageSQLScripts() 
		{ 
			// 
			// TODO: 在此处添加构造函数逻辑 
			// 
		} 
 
        ///  
        /// 读取SQL脚本文件,使用文件流,自动加目录 
        ///  
        /// 文件名,不带路径,如“EMIS_CreateTABSP.sql” 
        ///  
        public string ReadSQLScriptNO(string FileName) 
        { 
            try 
            { 
                string reValue = ""; 
                string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
                currAppPath += ".\\Resources\\Script\\"+FileName; 
                FileStream  fs  =  new  FileStream(@currAppPath,FileMode.Open,FileAccess.Read); 
                StreamReader  m_streamReader  =  new  StreamReader(fs)  ;   
                //使用StreamReader类来读取文件 
                m_streamReader.BaseStream.Seek(0,SeekOrigin.Begin)  ; 
                //  从数据流中读取每一行,直到文件的最后一行 
                string  strLine  =  m_streamReader.ReadLine()  ; 
                while(strLine!=null) 
                { 
                    reValue  +=  strLine  +  "\n"  ; 
                    strLine  =  m_streamReader.ReadLine()  ; 
                } 
                //关闭此StreamReader对象 
                return reValue; 
            } 
            catch(Exception  ex) 
            { 
                throw ex; 
            } 
        } 
 
        ///  
        /// 脚本写入文件,自动加目录 
        ///  
        /// 脚本 
        /// 文件名,不带路径,如“EMIS_CreateTABSP.sql” 
        ///  
        public void WriteSQLScriptNO(string FileName,string sqlString) 
        { 
            try 
            {                 
                string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
                currAppPath += ".\\Resources\\Script\\"+FileName; 
                //创建一个文件流,用以写入或者创建一个StreamWriter 
                FileStream  fs  =  new  FileStream(@currAppPath,FileMode.OpenOrCreate,FileAccess.Write)  ; 
                StreamWriter  m_streamWriter  =  new  StreamWriter(fs)  ; 
                m_streamWriter.Flush  (  )  ; 
     
                //  使用StreamWriter来往文件中写入内容 
                m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin)  ; 
                //  把对象中的内容写入文件 
                m_streamWriter.Write(sqlString)  ; 
                //关闭此文件 
                m_streamWriter.Flush()  ; 
                m_streamWriter.Close()  ; 
            } 
            catch(Exception  ex) 
            { 
                throw ex; 
            } 
        } 
 
        ///  
        /// 读去SQL脚本文件,使用文件流,不加目录 
        ///  
        /// 文件名,如“EMIS_CreateTABSP.sql” 
        ///  
        public string ReadSQLScript(string FileName) 
        { 
            StreamReader reader = null; 
            try 
            { 
                string reValue = ""; 
                string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
                currAppPath += ".\\Resources\\Script\\"+FileName; 
				if(System.IO.Path.IsPathRooted(FileName)) 
				{ 
					reader = new StreamReader(FileName,System.Text.Encoding.Default); 
				} 
				else 
				{ 
					reader = new StreamReader(currAppPath,System.Text.Encoding.Default); 
				} 
                reValue = reader.ReadToEnd(); 
                return reValue; 
            } 
            catch(Exception  ex) 
            { 
                throw ex; 
            } 
            finally 
            { 
                reader.Close(); 
            } 
        } 
 
        ///  
        /// 脚本写入文件,不加目录 
        ///  
        /// 脚本 
        /// 文件名,如“EMIS_CreateTABSP.sql” 
        ///  
        public void WriteSQLScript(string FileName,string sqlString) 
        { 
            System.IO.StreamWriter writer = null; 
			try 
			{                 
				string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase; 
				currAppPath += ".\\Resources\\Script\\"+FileName; 
                 
				sqlString = sqlString.Replace("\n","\r\n"); 
				if(System.IO.Path.IsPathRooted(FileName)) 
				{ 
					writer = new StreamWriter(FileName,false,System.Text.Encoding.UTF8); 
				} 
				else 
				{ 
					writer = new StreamWriter(currAppPath,false,System.Text.Encoding.UTF8);				 
				} 
				writer.Flush(); 
				writer.Write(sqlString);  
				writer.Flush(); 
				writer.Close();                
			} 
			catch(Exception  ex) 
			{ 
				throw ex; 
			} 
			finally 
			{ 
				writer.Close();     
			} 
        } 
 
	} 
}