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

📄 installer1.cs

📁 很不错的公文流转系统
💻 CS
📖 第 1 页 / 共 2 页
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.IO;
using System.DirectoryServices;

namespace OI
{
	/// <summary>
	/// Installer1 的摘要说明。
	/// </summary>
	[RunInstaller(true)]
	public class Installer1 : System.Configuration.Install.Installer
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string dbServer;//安装数据库的服务器地址
		private string dbUser;//管理员ID
		private string dbKey;//管理员密码
		private string dbName;//安装的数据库名称
		private string loginName;//数据库登录名
		private string loginPassword;//登录密码
		private string currentDir;//安装的虚拟目录的物理路径
		private string currentSystemDir;//系统目录,一般为c:\winnt\system32

		public Installer1()
		{
			// 该调用是设计器所必需的。
			InitializeComponent();

			// TODO: 在 InitializeComponent 调用后添加任何初始化
		}

		/// <summary> 
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		protected override void OnBeforeInstall(IDictionary savedState)
		{
			base.OnBeforeInstall (savedState);
			//获取参数
		

			dbServer=this.Context.Parameters["ServerName"];//安装数据库的服务器地址
			dbUser=this.Context.Parameters["InStallAdmin"];//管理员ID
			dbKey=this.Context.Parameters["InstallPassword"];//管理员密码
			dbName= this.Context.Parameters["DataBaseName"];//安装的数据库名称
			loginName=this.Context.Parameters["DataBaseName"];//数据库登录名
			loginPassword=System.Guid.NewGuid().ToString();//随机登录密码
			currentDir=this.Context.Parameters["dir"];//安装的虚拟目录的物理路径
			if(currentDir.Substring(currentDir.Length-1,1)=="\\")
				currentDir=currentDir.Substring(0,currentDir.Length-1);
			//currentSystemDir=System.IO.Directory.GetCurrentDirectory();
			currentSystemDir=Environment.GetFolderPath(Environment.SpecialFolder.System);//系统目录,一般为c:\winnt\system32

			//保存密码
			this.SavePassword(loginPassword);


		}

		private void SavePassword(string password)
		{
			System.IO.StreamWriter streamPassword=new System.IO.StreamWriter(System.IO.Directory.GetCurrentDirectory()+@"\PowerOI\BCEC709A-9639-409b-9EC5-194E6EF5F64F.ini");
			streamPassword.Write(password);
			streamPassword.Close();
		}
		private string GetPassword()
		{
			string c;
			System.IO.StreamReader streamPassword=new System.IO.StreamReader(System.IO.Directory.GetCurrentDirectory()+@"\PowerOI\BCEC709A-9639-409b-9EC5-194E6EF5F64F.ini");
			c=streamPassword.ReadToEnd();
			streamPassword.Close();
			return c;
		}

		private void RunCommand(string sql,System.Data.SqlClient.SqlConnection conn)
		{
			System.Data.SqlClient.SqlCommand command=new System.Data.SqlClient.SqlCommand(sql,conn);
			if(conn.State!=System.Data.ConnectionState.Open)
				conn.Open();
			try
			{
				command.ExecuteNonQuery();
			}
			catch(System.Data.SqlClient.SqlException ee)
			{
				OI.Modules.ErrorLog.WriteLog("执行下列SQL语句出错:"+sql);
				if(conn.State!=System.Data.ConnectionState.Closed)
					conn.Close();
				throw ee;
			}
			command.Dispose();
			if(conn.State!=System.Data.ConnectionState.Closed)
				conn.Close();

			
		}
		public override void Install(IDictionary stateSaver)
		{
			this.OnBeforeInstall(stateSaver);
			base.Install (stateSaver);
			dbServer=this.Context.Parameters["ServerName"];//安装数据库的服务器地址
			dbUser=this.Context.Parameters["InStallAdmin"];//管理员ID
			dbKey=this.Context.Parameters["InstallPassword"];//管理员密码
			dbName= this.Context.Parameters["DataBaseName"];//安装的数据库名称
			loginName=this.Context.Parameters["DataBaseName"];//数据库登录名
			loginPassword=this.GetPassword();//随机登录密码
			currentDir=this.Context.Parameters["dir"];//安装的虚拟目录的物理路径
			if(currentDir.Substring(currentDir.Length-1,1)=="\\")
				currentDir=currentDir.Substring(0,currentDir.Length-1);
			//currentSystemDir=System.IO.Directory.GetCurrentDirectory();//系统目录,一般为c:\winnt\system32
			currentSystemDir=Environment.GetFolderPath(Environment.SpecialFolder.System);//系统目录,一般为c:\winnt\system32

			


			System.Data.SqlClient.SqlConnection conn;
			conn=new System.Data.SqlClient.SqlConnection();
			conn.ConnectionString="server="+dbServer+";database=master;uid="+dbUser+";pwd="+dbKey;
			
			string bakDatabaseName=dbName+System.DateTime.Now.Year.ToString()+System.DateTime.Now.Month.ToString()+System.DateTime.Now.Day.ToString()+System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString()+System.DateTime.Now.Second.ToString();

			//开始安装数据库



			//构造sql语句
			string sql="";
			
			

			
		
			//运行sql语句
			//备份原同名数据库
			sql=" use master "+
				" IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'"+this.dbName+"') "+
				" begin "+
				" EXEC master..sp_addumpdevice 'disk', '"+bakDatabaseName+"','"+this.currentSystemDir+@"\"+bakDatabaseName+".bak' "+
				" backup database "+this.dbName+" to "+bakDatabaseName+" "+				
				" end ";
			this.RunCommand(sql,conn);
			//终止原有数据库的所有进程并删除原有数据库
			sql=" use master "+
				" declare @spid nvarchar(20) "+
				" declare #tb cursor for "+
				" 	select spid=cast(spid as varchar(20)) from master..sysprocesses where dbid=db_id('"+this.dbName+"') "+
				" open #tb "+
				" fetch next from #tb into @spid "+
				" while @@fetch_status=0 "+
				" begin   "+
				" 	exec('kill '+@spid) "+
				" 	fetch next from #tb into @spid "+
				" end   "+
				" close #tb "+
				" deallocate #tb "+
				" IF EXISTS (SELECT name FROM master.dbo.sysdatabases WHERE name = N'"+this.dbName+"') "+
				" begin "+
				" DROP DATABASE ["+this.dbName+"] "+
				" end ";
			this.RunCommand(sql,conn);
			//建立新数据库
			sql=" CREATE DATABASE ["+this.dbName+"]   collate  CHINESE_PRC_CI_AS ";
			
			try
			{
				this.RunCommand(sql,conn);
			}
			catch(System.Data.SqlClient.SqlException e)
			{
				//安装数据库失败,还原数据库
				sql=" restore database "+this.dbName+" from "+bakDatabaseName+"  ";
				this.RunCommand(sql,conn);
				throw e;
			}
			//新建登录
			
			sql=" use ["+this.dbName+"] "+
				" if  exists (select * from master.dbo.syslogins where loginname = N'"+this.loginName+"') "+
				" BEGIN "+
				//" EXEC sp_revokedbaccess '"+this.loginName+"' "+
				"	EXEC sp_droplogin '"+this.loginName+"' "+				
				" END "+
				" exec sp_addlogin N'"+this.loginName+"', N'"+this.loginPassword+"',N'"+this.dbName+"' "+
				" if not exists (select * from dbo.sysusers where name = N'"+this.loginName+"' and uid < 16382) "+
				" 	EXEC sp_grantdbaccess N'"+this.loginName+"', N'"+this.loginName+"' "+
				" exec sp_addrolemember N'db_owner', N'"+this.loginName+"' ";
			
			this.RunCommand(sql,conn);

			conn.ConnectionString="server="+this.dbServer+";database="+this.dbName+";uid="+this.dbUser+";pwd="+this.dbKey;
			
			//建立表及相关对象
			//读取sql语句
			System.Xml.XmlDocument oXmlDocument=new System.Xml.XmlDocument();
			oXmlDocument.Load(this.currentSystemDir+@"\PowerOI\sql.xml");

			string nodes="//SQL";
			
			foreach(System.Xml.XmlNode oXmlNode in oXmlDocument.DocumentElement.SelectNodes(nodes))
			{
				sql=oXmlNode.InnerText;
				if(sql=="")
					continue;
				this.RunCommand(sql,conn);
			}
			
			//初始化数据
			oXmlDocument.Load(this.currentSystemDir+@"\PowerOI\data.xml");
			System.Xml.XmlElement rootElement=oXmlDocument.DocumentElement;
			string str="/Tables/Table";
			
			System.Data.SqlClient.SqlCommandBuilder oSqlCommandBuilder=new System.Data.SqlClient.SqlCommandBuilder();
			System.Data.SqlClient.SqlDataAdapter oSqlDataAdapter;
			
			

			
			System.Data.DataSet odataset=new System.Data.DataSet();
			string FieldNameList="";
			string TableName;
			if(conn.State!=System.Data.ConnectionState.Open)
				conn.Open();
			foreach(System.Xml.XmlNode onode in rootElement.SelectNodes(str))
			{
				TableName=onode.Attributes["Name"].Value;
				foreach(System.Xml.XmlNode ofield in onode.ChildNodes)
				{
					if(FieldNameList=="")

⌨️ 快捷键说明

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