VC#打包全攻略之(进一步修改中)

 

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


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


        

        

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值