VC#打包全攻略之(进一步修改中)
来源:互联网 发布:oracle数据库应用案例 编辑:程序博客网 时间:2024/06/05 15:40
using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Xml;
namespace Install
...{
/**//// <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
//---------------------------------------------------------------------------------------------------------------------------------
//执行sql 语句
private void ExecuteSql(string conn, string DatabaseName, string Sql)
...{
SqlConnection mySqlConnection=new SqlConnection(conn);
SqlCommand Command=new SqlCommand(Sql,mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
...{
Command.ExecuteNonQuery();
}
finally
...{
mySqlConnection.Close();
Command.Connection.Close();
}
}
public override void Install(IDictionary stateSaver)
...{
base.Install (stateSaver);
//------------------------建立数据库-------------------------------------------------
try
...{
string connStr=string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
//根据输入的数据库名称建立数据库
ExecuteSql(connStr, "TestDB", "CREATE DATABASE TestDB");
//调用osql执行脚本
Process sqlProcess=new Process();
sqlProcess.StartInfo.FileName="osql.exe";
sqlProcess.StartInfo.Arguments=string.Format(" -U {0} -P {1} -d {2} -i {3}TestDB.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();
//删除脚本文件
FileInfo sqlFileInfo=new FileInfo(string.Format("{0}TestDB.sql", this.Context.Parameters["targetdir"]));
if(sqlFileInfo.Exists)
sqlFileInfo.Delete();
}
catch(Exception ex)
...{
throw ex;
}
// ---------------------将连接字符串写入Web.config-----------------------------------
try
...{
FileInfo fileInfo=new FileInfo(this.Context.Parameters["targetdir"]+"/web.config");
if(fileInfo.Exists)
//throw InstallException("没有找到配置文件");
//实例化XML文档
XmlDocument xmlDocument=new XmlDocument();
xmlDocument.Load(fileInfo.FullName);
//查找到appSettings中的节点
//XmlNode node=new XmlNode();
bool FoundIt=false;
foreach(XmlNode node in xmlDocument["configuration"]["appSettings"])
...{
if(node.Name="add")
...{
if(node.Attributes.GetNamedItem("key").Value=="connString")
...{
//写入连接字符串
node.Attributes.GetNamedItem("value").Value=string.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", this.Context.Parameters["server"], this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
FoundIt=true;
}
}
}
if(FoundIt==false)
//throw InstallException("web.Config 文件没有包含connString连接字符串设置");
xmlDocument.Save(fileInfo.FullName);
}
catch(Exception ex)
...{
throw ex;
}
//---------------------------------------------------------------------------------------------------------------------------------
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using System.Diagnostics;
using System.IO;
using System.Xml;
namespace Install
...{
/**//// <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
//---------------------------------------------------------------------------------------------------------------------------------
//执行sql 语句
private void ExecuteSql(string conn, string DatabaseName, string Sql)
...{
SqlConnection mySqlConnection=new SqlConnection(conn);
SqlCommand Command=new SqlCommand(Sql,mySqlConnection);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try
...{
Command.ExecuteNonQuery();
}
finally
...{
mySqlConnection.Close();
Command.Connection.Close();
}
}
public override void Install(IDictionary stateSaver)
...{
base.Install (stateSaver);
//------------------------建立数据库-------------------------------------------------
try
...{
string connStr=string.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
//根据输入的数据库名称建立数据库
ExecuteSql(connStr, "TestDB", "CREATE DATABASE TestDB");
//调用osql执行脚本
Process sqlProcess=new Process();
sqlProcess.StartInfo.FileName="osql.exe";
sqlProcess.StartInfo.Arguments=string.Format(" -U {0} -P {1} -d {2} -i {3}TestDB.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();
//删除脚本文件
FileInfo sqlFileInfo=new FileInfo(string.Format("{0}TestDB.sql", this.Context.Parameters["targetdir"]));
if(sqlFileInfo.Exists)
sqlFileInfo.Delete();
}
catch(Exception ex)
...{
throw ex;
}
// ---------------------将连接字符串写入Web.config-----------------------------------
try
...{
FileInfo fileInfo=new FileInfo(this.Context.Parameters["targetdir"]+"/web.config");
if(fileInfo.Exists)
//throw InstallException("没有找到配置文件");
//实例化XML文档
XmlDocument xmlDocument=new XmlDocument();
xmlDocument.Load(fileInfo.FullName);
//查找到appSettings中的节点
//XmlNode node=new XmlNode();
bool FoundIt=false;
foreach(XmlNode node in xmlDocument["configuration"]["appSettings"])
...{
if(node.Name="add")
...{
if(node.Attributes.GetNamedItem("key").Value=="connString")
...{
//写入连接字符串
node.Attributes.GetNamedItem("value").Value=string.Format("Persist Security Info=False;Data Source={0};Initial Catalog={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", this.Context.Parameters["server"], this.Context.Parameters["dbname"], this.Context.Parameters["user"], this.Context.Parameters["pwd"]);
FoundIt=true;
}
}
}
if(FoundIt==false)
//throw InstallException("web.Config 文件没有包含connString连接字符串设置");
xmlDocument.Save(fileInfo.FullName);
}
catch(Exception ex)
...{
throw ex;
}
//---------------------------------------------------------------------------------------------------------------------------------
}
}
}
- VC#打包全攻略之(进一步修改中)
- VC#打包部署全攻略之添加卸载程序
- VC#打包部署全攻略之(一、添加卸载程序)
- vc#打包部署全攻略之(二、自动添加.NET Framework)
- AS打包全攻略
- C#打包部署全攻略之添加卸载程序
- AS打包全攻略-简
- AS打包全攻略-繁
- VC中DDB与DIB位图编程全攻略
- VC中DDB与DIB位图编程全攻略
- VC中DDB与DIB位图编程全攻略
- VC中DDB与DIB位图编程全攻略
- VC中DDB与DIB位图编程全攻略
- VC字节对齐全攻略
- -VC字节对齐全攻略
- VC用ADO访问数据库全攻略(转载)
- SOA之全攻略
- Ant全攻略4 ----- 打包相关任务
- MS-VC 使用MAP文件快速定位程序崩溃代码行
- 存储过程编写经验和优化措施
- 清除记录的一些方法
- Fedora core5中添加源sourcelist,使用yum管理包
- 五个常见 PHP 数据库问题
- VC#打包全攻略之(进一步修改中)
- 文件批处理器 / bat_do 说明与下载 (V0.0.0002)
- 去除left join fetch中的多余记录
- 企业校长PK大学校长——致大学校长的公开信
- 今年印度国际竞争力超过中国11位。
- linux解压方法
- 巴顿将军战前动员讲话
- 皓月 赠老婆 (机器生成的)
- 使用 ANT 开发 Java 程序