10. 最后还要保证我们项目运行所必须的环境(.Net Framework等)
右键单击安装项目的项目名,进入属性

点击系统必备

11. 在DBClassLiabrary类库的MyInstaller.cs文件中编写安装数据库的代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data.SqlClient;
using Microsoft.Win32;
namespace DBAction
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
public MyInstaller()
{
InitializeComponent();
}
private void ExecuteSql(string connString, string DBName, string sqlString)
{
SqlConnection conn = new SqlConnection(connString);
SqlCommand comm = new SqlCommand(sqlString, conn);
try
{
conn.Open();
conn.ChangeDatabase(DBName);//更改当前数据库
comm.ExecuteNonQuery();
}
catch (SqlException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
conn.Close();
}
}
//重写Install方法
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",
this.Context.Parameters["server"], this.Context.Parameters["User"],
this.Context.Parameters["pwd"]);
ExecuteSql(connStr, "master", "create database " + this.Context.Parameters["dbname"]);
//创建一个进程用来执行sql脚本---建表
System.Diagnostics.Process sqlProcess = new System.Diagnostics.Process();
//设置该进程要启动的文档是"osql.exe",这个执行文件用来在命令行执行sql脚本文件
sqlProcess.StartInfo.FileName = "osql.exe";
//配置进程需要的参数
//Context.Parameters--获取在运行InstallUtil.exe时输入的命令行参数
sqlProcess.StartInfo.Arguments = string.Format("-U {0} -P {1} -i {2}bankDB.sql",
this.Context.Parameters["user"], this.Context.Parameters["pwd"],
this.Context.Parameters["targetdir"]);
sqlProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
}
catch (Exception ex)
{
Console.Write(ex.ToString());
}
}
/// <summary>
/// 重载OnBeforeInstall,确定是否安装.NETFramework
/// </summary>
protected override void OnBeforeInstall(System.Collections.IDictionary savedState)
{
base.OnBeforeInstall(savedState);
try
{
Microsoft.Win32.RegistryKey key;//定义注册表键
//读取相同位置上的信息,若key为空,则不存在此键值,则进行安装;相反为存在则不安装
key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\.NETFramework", true);
if (key == null)
{
Process.Start(Context.Parameters["targetdir"].ToString() + @" dotnetfx \dotnetfx.exe");//得到安装后文件的路径,并通过路径和文件名来启动
}
}
catch (Exception e)//抓取错误信息,并给予提示
{
MessageBox.Show("安装程序错误!错误提示:" + e.Message);
}
}
}
}
提示:
在bankDB.sql脚本文件中最好加上 USE bankDB GO ,为了使安装程序程序能够正确运行,必须确保你的bankDB.sql脚本文件准确无误,检查方法就是在命令行中输入:C:> osql –U sa –P 123 –i D:\打包示例\bankDB.sql 如果在你自己的机器上能生成数据库bankDB,就说明sql脚本没错
12. 整个解决方案全部完成后,请分别生成3个项目的执行文件,确保得到最新结果。其中在MyProjSetup的Debug文件夹中的就是我们需要的结果

我们双击setup.exe或者MyProjSetup.msi之后,就能够在目标计算机上安装bank数据库和我们自己的MyProjectV2的执行文件了
我的目标文件选择的是D:\SetupDemo,最后在该文件夹下的文件如下:

可以看到在目标机器上的目标路径下包含了项目所需要的各个类库、文件等等,以及自己的执行文件

/dbname=[EDITA1] /server=[EDITA2] /user = [EDITA3] /pwd = [EDITA4] /targetdir="[TARGETDIR]\" -------错误
/dbname=[EDITA1] /server=[EDITA2] /user=[EDITA3] /pwd=[EDITA4] /targetdir="[TARGETDIR]\" ------- 正确
切记切记:/key=[value] 之间绝对不能有空格
最后需要在目标机器上安装.net framework.另外app.Config配置文件也可以打包,而且在打包后会自动出现在目标机器的目标文件夹,因此并不存在打包后无法修改配置文件的情况。
<appSettings>
<add key=”connStr”value=”………”>
</ appSettings >
注意需要添加引用System.Configration
点击系统必备
选择你需要的组件,如.Net Framework2.0 和水晶报表组件等
本文转自 qianshao 51CTO博客,原文链接:http://blog.51cto.com/qianshao/202498,如需转载请自行联系原作者