C#打包SQL数据库部署安装

来源:互联网 发布:新浪微博 mac 编辑:程序博客网 时间:2024/06/05 01:00

C#的类库代码如下:DBCustomAction.cs

using System;
using System.Collections;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Reflection;


namespace PMS
{
 /// <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 调用后添加任何初始化
  }

  private void ExecuteSql(string conn,string DatabaseName,string Sql)
  {
   SqlConnection mySqlConnection=new SqlConnection(conn);  
   SqlCommand Command=new SqlCommand(Sql, mySqlConnection);  
   mySqlConnection.Open();  
   mySqlConnection.ChangeDatabase(DatabaseName);  
   try
   {
    Command.ExecuteNonQuery();
   }  
   finally
   {
    //close Connection  
    mySqlConnection.Close();
   }
  }

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

  //

  public override void Install(System.Collections.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", Context.Parameters["server"],Context.Parameters["user"], Context.Parameters["pwd"]);
    //'根据输入的数据库名称建立数据库  
    ExecuteSql(connstr, "master", "CREATE DATABASE " +Context.Parameters["dbname"]);  
    //'调用osql执行脚本  
    Process sqlprocess=new System.Diagnostics.Process();
    sqlprocess.StartInfo.FileName = "osql.exe ";  
    sqlprocess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql", Context.Parameters["user"], Context.Parameters["pwd"],Context.Parameters["dbname"],Context.Parameters["targetdir"]);
    sqlprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    sqlprocess.Start();  
    sqlprocess.WaitForExit(); // '等待执行
    sqlprocess.Close();
  
    //'删除脚本文件
    FileInfo sqlfileinfo =new FileInfo(String.Format("{0}db.sql",Context.Parameters["targetdir"]));
  
    if (sqlfileinfo.Exists)
    {
     sqlfileinfo.Delete();
    }
   }
   catch(Exception ex)
   {
    throw ex;  
   }
  
   //' ---------------------将连接字符串写入Web.config-----------------------------------
  /*
   try
   {
    FileInfo fileinfo = new FileInfo(Context.Parameters["targetdir"] + "\\web.config");
    if (!fileinfo.Exists)
    {
     throw new InstallException("没有找到配置文件");
  
    }
  
    //'实例化xml文档
  
    XmlDocument xmldocument=new XmlDocument();
  
    xmldocument.Load(fileinfo.FullName);
  
  
  
    //'查找到appsettings中的节点
  
    //XmlNode node=new XmlNode();
  
    Boolean FoundIt  = false;
  
    foreach(XmlNode node in xmldocument.SelectSingleNode("appSettings").ChildNodes)
    {  
     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",Context.Parameters["server"],Context.Parameters["dbname"], Context.Parameters["user"], Context.Parameters["pwd"]);
       FoundIt= true;  
      }  
     }  
    }
  
    if (!FoundIt)
    {  
     throw new InstallException("web.Config 文件没有包含connString连接字符串设置");
    }   
    xmldocument.Save(fileinfo.FullName);
   }
   catch(Exception ex)
   {
    throw ex;
   } 
   */         
  }

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

我不需要修改Web.config的部分.
注意.如果不用SA用户登录数据库的,请先在服务器上建立特定的SQL用户

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 喉咙总感觉有痰怎么办 感冒了喉咙有痰怎么办 咽喉有异物感是怎么办 老感觉喉咙有痰怎么办 感冒有痰怎么办最有效 感冒快好了有痰怎么办 喉咙里一直有痰怎么办 一到晚上就咳嗽怎么办 1岁宝宝咳嗽痰多怎么办 3岁宝宝咳嗽痰多怎么办 六岁儿童咳嗽有痰怎么办 很多白痰在喉咙怎么办 我喉咙总是有痰怎么办 喉咙老感觉有痰怎么办 喉咙痒老是有痰怎么办 抽烟多了嗓子疼怎么办 抽烟多了老咳嗽怎么办 抽烟抽多了咳嗽怎么办 嗓子咳出异物臭怎么办 鼻子有鼻涕喉咙有痰怎么办 怀孕39周感冒了怎么办 一口痰卡在喉咙怎么办 鼻塞黄鼻涕黄痰怎么办 小孩咳嗽流黄鼻涕怎么办 小孩鼻塞怎么办最简单方法 有黄鼻涕黄痰怎么办 咳嗽有泡沫白痰怎么办 痰多咳嗽老不好怎么办 5岁儿童咳嗽有痰怎么办 感冒后一直有痰怎么办 感冒吐绿色的痰怎么办 孕妇咳嗽有痰怎么办啊 没结婚的人死了怎么办 金花鼠尾巴断了怎么办 辞职交了不批怎么办 离职了又想回去怎么办 想辞职领导不批怎么办 急辞职领导不批怎么办 她生气不理我了怎么办 分手了之前的钱怎么办 结婚后老公变了怎么办