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 );
        }



        
组件设计器生成的代码
        
//---------------------------------------------------------------------------------------------------------------------------------
        
//执行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;
            }


            
//---------------------------------------------------------------------------------------------------------------------------------
        }


        

        

    }

}

原创粉丝点击