我爱小虾 发表于 2016-11-7 09:18:22

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

在.NET程序安装包中附带 SQL SERVER数据库安装功能
左直拳
一、 安装文件夹中添加数据库安装程序dbInstall.dll以及建数据库脚本data.sql。dbInstall是一个自己编写的DLL,代码见附录。
二、 添加自定义功能。
方法:选定安装项目,鼠标右键,弹出快捷菜单,选视图-自定义操作。也可以直接点“解决方案资源管理器”上部的快捷图标。
进入“自定义操作”视图后,在“安装”目录下添加自定义操作,对应之前添加的dbInstall,命名为“安装数据库”。编辑属性,
CustomerActionData:  

/targetdir= /dbfile="data.sql"  

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

附录:  

dbInstall.dll  

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

1、信息采集界面部分(dbpara.cs),采集必要的数据库安装信息,如下(不知道为什么,无法上传图片):
服务器 默认为(local)
数据库管理员帐户 默认为sa
数据库管理员密码
待安装数据库名称 自己填写

  

<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 306pt; HEIGHT: 198pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/chenqu/LOCALS~1/Temp/msoclip1/01/clip_image001.png" o:title=""></imagedata></shape>  

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

}  

}  
页: [1]
查看完整版本: 在.NET程序安装包中附带 SQL SERVER数据库安装功能