您的位置:首页 > 其它

vs2005 安装部署打包类

2007-12-21 14:48 260 查看

using System;


using System.Collections.Generic;


using System.ComponentModel;


using System.Configuration.Install;


using System.IO;


using System.Data.SqlClient;


using System.Reflection;




namespace DBInstall




...{


[RunInstaller(true)]


public partial class Installer2 : Installer




...{


public Installer2()




...{


InitializeComponent();


}




private System.ComponentModel.Container components = null;


//private string strPass = "";


private string server = "",user="",pwd="",dbname="";














Component Designer generated code#region Component Designer generated code




/**//// <summary>


/// 设计器支持所需的方法 - 不要使用代码编辑器修改


/// 此方法的内容。


/// </summary>


private void InitializeComponent()




...{




}


#endregion




public override void Install(System.Collections.IDictionary stateSaver)




...{




//入口




dbname= this.Context.Parameters["dbname"]; //dbname 为数据库名称


server = this.Context.Parameters["server"]; //server 为 服务器名称


user = this.Context.Parameters["user"]; //user 为 数据库登录名称


pwd = this.Context.Parameters["pwd"]; //pwd 为 数据库 登录密码




AddDBTable(dbname); //添加表,视图,存储过程


WriteWebConfig(); //更新webconfig 连接字符串




}


private string GetSql(string strName)




...{


try




...{


//' Get the current assembly.


Assembly Asm = Assembly.GetExecutingAssembly();


// Resources are named using a fully qualified name




Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + strName);




//Read the contents of the embedded file.


StreamReader reader= new StreamReader(strm);//,System.Text.Encoding.Unicode);






return reader.ReadToEnd();


}


catch




...{


return null;


}




}






/**//// <summary>


/// 执行sql 语句


/// </summary>


/// <param name="DatabaseName"></param>


/// <param name="Sql"></param>


private void ExecuteSql(string DatabaseName , string Sql)




...{




string connString=string.Format("server={0};user id={1};password={2};database={3}",server,user,pwd,DatabaseName);






SqlConnection sqlConnection1 = new SqlConnection(connString) ;


SqlCommand Command = new SqlCommand(Sql, sqlConnection1);


Command.Connection.Open();


Command.Connection.ChangeDatabase(DatabaseName);




try




...{


Command.ExecuteNonQuery();


}




finally




...{


// Finally, blocks are a great way to ensure that the connection


Command.Connection.Close();


}




}


protected void AddDBTable(string strDBName )




...{


try




...{


//Create the database.


ExecuteSql("master", "CREATE DATABASE " + strDBName);


// Create the tables.


ExecuteSql(strDBName, GetSql("sql.txt"));


}


catch




...{




}


}






/**//// <summary>


///数据库安装完成后更新webconfig 文件


/// </summary>


/// <returns></returns>


private bool WriteWebConfig()




...{




System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/web.config");


if (!FileInfo.Exists)




...{




throw new InstallException("Missing config file:" + this.Context.Parameters["targetdir"] + "/web.config");


}


System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();


xmlDocument.Load(FileInfo.FullName);


bool FoundIt = false; //设置变量 初始值false 表示 更新不成功


foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])




...{




if (Node.Name == "add")




...{




if (Node.Attributes.GetNamedItem("name").Value == "sweaterConnectionString")




...{




//Node.Attributes.GetNamedItem("value").Value = String.Format("Persist Security Info=False;Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", ServerName, DBName, AdminName, AdminPwd);


Node.Attributes.GetNamedItem("connectionString").Value = string.Format("server={0};database={1};uid={2};password={3};Max Pool Size=512",server,dbname,user,pwd);


FoundIt = true;




}




}




}








if (!FoundIt)




...{




throw new InstallException("Error when writing the config file: web.config");




}








xmlDocument.Save(FileInfo.FullName);




return FoundIt;




}


}


}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: