在.NET程序安装包中附带 SQL SERVER数据库安装功能

.NET程序安装包中附带 SQL SERVER数据库安装功能

左直拳

一、        安装文件夹中添加数据库安装程序dbInstall.dll以及建数据库脚本data.sqldbInstall是一个自己编写的DLL,代码见附录。

二、        添加自定义功能。

方法:选定安装项目,鼠标右键,弹出快捷菜单,选视图-自定义操作。也可以直接点“解决方案资源管理器”上部的快捷图标。

进入“自定义操作”视图后,在“安装”目录下添加自定义操作,对应之前添加的dbInstall,命名为“安装数据库”。编辑属性,

CustomerActionData

/targetdir=[TARGETDIR] /dbfile="data.sql"

这是传递给数据库安装程序dbInstall.dll的参数。

附录:

dbInstall.dll

这是一个Windows应用程序。主要由两部分组成。

1、信息采集界面部分(dbpara.cs),采集必要的数据库安装信息,如下(不知道为什么,无法上传图片):

服务器               默认为(local)
数据库管理员帐户     默认为sa
数据库管理员密码
待安装数据库名称     自己填写

2、数据库安装部分(组件类dbInstall.cs

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

{

     base.Install(stateSaver);

 

     string dbFile = this.Context.Parameters["dbfile"];

     if( dbFile == null || this.Context.Parameters["installstyle"] == null )

     {

         return;

     }

 

     //创建数据库

     CREATEDB:

     //激活信息采集界面

     dbInstall.FrmDbpara dbpara = new dbInstall.FrmDbpara();

     if( dbpara.ShowDialog() == DialogResult.Cancel )

     {

         throw(new InstallException("安装失败。"));

     }

     string server = dbpara.Server;//服务器

     string user = dbpara.User;    //数据库管理员帐号,如sa

     string pwd = dbpara.Pwd;      //数据库管理员帐号密码

     string db = dbpara.Db;        //待建立的数据库名

     dbpara.Dispose();

//开始安装数据库

     string strConn = String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096", server,user,pwd);

     string strCreateDB = String.Format("CREATE DATABASE {0} ON (NAME={0}_data,FILENAME=/'{1}data//{0}_data.mdf/') LOG ON (NAME={0}_log,FILENAME=/'{1}data//{0}_log.ldf/')",db,this.Context.Parameters["targetdir"]);

     try

     {

         ExecuteSql(strConn, "master", strCreateDB);

         System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();

         sqlProcess.StartInfo.FileName = "osql.exe";

         sqlProcess.StartInfo.Arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}" + dbFile,user,pwd,db,this.Context.Parameters["targetdir"]);

         sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

         sqlProcess.Start();

         sqlProcess.WaitForExit();

         sqlProcess.Close();

         //把数据库脚本文件删掉

         System.IO.FileInfo fileSql = new System.IO.FileInfo(String.Format("{0}" + dbFile, this.Context.Parameters["targetdir"]));

         if( fileSql.Exists )

         {

              fileSql.Delete();

         }

     }

     catch( Exception ex )

     {

         switch( MessageBox.Show(ex.Message,"错误",MessageBoxButtons.AbortRetryIgnore,MessageBoxIcon.Exclamation) )

         {

              case DialogResult.Retry:

                   goto CREATEDB;

              case DialogResult.Abort:

                   throw(new InstallException("安装失败。"));

              case DialogResult.Ignore:

                   break;

              default:

                   throw(new InstallException(ex.Message + "/n安装失败。"));

         }

     }

 

}

 

/// <summary>

/// 执行SQL

/// </summary>

/// <param name="strConn"></param>

/// <param name="dbname"></param>

/// <param name="sql"></param>

private void ExecuteSql(string strConn,string dbname,string sql)

{

     SqlConnection conn = new SqlConnection(strConn);

     SqlCommand cmd = new SqlCommand(sql,conn);

     cmd.Connection.Open();

     cmd.Connection.ChangeDatabase(dbname);

     try

     {

         cmd.ExecuteNonQuery();

     }

     catch(Exception ex)

     {

         MessageBox.Show(ex.Message);

     }

     finally

     {

         conn.Close();

     }

}

原文地址:https://www.cnblogs.com/leftfist/p/4258359.html