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

📄 dbcustomaction.cs

📁 前台:asp.net;后台: sql server 一个功能完善的BBS系统源码。
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.IO;
using System.Reflection;
using System.Diagnostics;
using System.Xml;

namespace DBCustomAction
{
	/// <summary>
	/// DBCustomAction 的摘要说明。
	/// </summary>
	[RunInstaller(true)]
	public class DBCustomAction : System.Configuration.Install.Installer
	{
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

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

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

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


		#region 组件设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion

		#region custom setup



		private void ExecuteSql(string connString,string DatabaseName,string sql)
		{
			SqlConnection conn=new SqlConnection(connString);
			SqlCommand cmd=new SqlCommand(sql,conn);
			conn.Open();
			cmd.Connection.ChangeDatabase(DatabaseName);
			try
			{
				cmd.ExecuteNonQuery();
			}
			catch(Exception e)
			{
				StreamWriter w=new StreamWriter(@"e:\\log.txt",true);
				w.WriteLine("===in ExecuteSql======");
				w.WriteLine(e.ToString());
				w.Close();
			}
			finally
			{
				conn.Close();
			}
		}

		public override void Install(IDictionary stateSaver)
		{
			createDB();
			updateConfig();
		}

		private void createDB()
		{
			try
			{
				string connString=string.Format("server={0}; user id={1}; password={2};database=master",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"]);

				//根据输入的数据库名称建立数据库
				ExecuteSql(connString,"master","create database "+this.Context.Parameters["dbname"]);
				//调用osql执行脚本
				System.Diagnostics.Process sqlProcess=new Process();
				sqlProcess.StartInfo.FileName="osql.exe";
				sqlProcess.StartInfo.Arguments=string.Format(" -U{0} -P{1} -d{2} -i{3}db.sql",this.Context.Parameters["user"],this.Context.Parameters["pwd"],this.Context.Parameters["dbname"],this.Context.Parameters["targetdir"]);
				sqlProcess.StartInfo.WindowStyle=ProcessWindowStyle.Hidden;
				sqlProcess.Start();
				sqlProcess.WaitForExit();//等待执行
				sqlProcess.Close();

				//删除脚本文件
				System.IO.FileInfo sqlFileInfo=new FileInfo(string.Format("{0}db.sql",this.Context.Parameters["targetdir"]));
				if(sqlFileInfo.Exists)
					sqlFileInfo.Delete();
			}
			catch(Exception e)
			{
				StreamWriter w=new StreamWriter(@"e:\log.txt",true);
				w.WriteLine("===in Install======");
				w.WriteLine(e.ToString());
				w.Close();
			}
		}

		private void updateConfig()
		{
			try
			{
				//将连接字符串写入Web.config
				System.IO.FileInfo fileInfo=new FileInfo(string.Format("{0}web.config",this.Context.Parameters["targetdir"]));

				if(!fileInfo.Exists)
					throw new InstallException("can't find the web.config");

				XmlDocument doc=new XmlDocument();
				doc.Load(fileInfo.FullName);
				bool foundIt=false;

				string connString=string.Format("server={0}; user id={1}; password={2};database={3}",this.Context.Parameters["server"],this.Context.Parameters["user"],this.Context.Parameters["pwd"],this.Context.Parameters["dbname"]);

				string enCS=SecurityHelper.EncryptDBConnectionString(connString);

				XmlNode no=doc.SelectSingleNode("//appSettings/add[@key='connString']");
				if(no!=null)
				{
					no.Attributes.GetNamedItem("value").Value=enCS;
					foundIt=true;
				}

				if(!foundIt)
					throw new InstallException("can't find the connString setting ");
				doc.Save(fileInfo.FullName);
			}
			catch(Exception e)
			{
				StreamWriter w=new StreamWriter(@"e:\log.txt",true);
				w.WriteLine("===in updata connstring=tjtj=====");
				w.WriteLine(e.ToString());
				w.WriteLine(e.StackTrace);
				w.Close();
			}
		}
		
		#endregion
	}
}

⌨️ 快捷键说明

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