4.
点击安装工程选择视图,自定义操作。右键安装文件夹,选择添加自定义操作,如果你的上面的工程输出中有自定义类的输出那么双击应用程序文件夹选则数据库的工程输出就行。如果没有那么添加家输出选择自定义类的数据库的工程就行。然后确定,如图:
5.
设置关于安装程序的其他属性咱们暂且不提,这是可以对你的安装工程进行编译了,当编译通过后,你可以在MySetup1/Debug/看到你的打包工程MySetup.msi。
6 .
这是工程打包告一段落,下面我们需要修改打好的安装包,使它可以在安装完程序后自动安装MSDE的一个实例(假设实例名为:MyServer)。现在我们就用到MS的一个工具ORCA,然后就可以利用这个工具对我们的MySetup1.msi进行修改了。
InstallExecuteSequence表
GetSqlStates.XXXXXX 103->421
InstallInitialize
1800->1799
RemoveExistingProducts
6850->1800
InstallUISequence表
GetSqlStates.XXXXXX
103->421
Property表添加
SqlInstanceName:
MSDEDH实例服务名
SqlSecurityMode:
SQL
用SQL模式登录
SqlSaPwd:
sa的密码
DISABLENETWORKPROTOCOLS=0:网络访问的话也要加这项
注意:GetSqlStates.XXXXXX
后面的X代表一串字符。例如:我的里面是,GetSqlStates.2D02443E_7002_4C0B_ABC9_EAB2C064397B
你主要是找到GetSqlStates这部分。
SqlInstanceName表示的是你所要附加的这个数据库是要附加到哪个实例的名称,并且在安装msde是开启该实例的服务。SqlSecurityMode表示用什么模式登陆,我用的是SQL的模式,那么它的值就是SQL。SqlSaPwd表示的是用SQL登陆你的sa账户的密码。
8.
至于附加数据库 自己做一个 安装类,进行自定义安装时附加数据库就行了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Diagnostics;
using System.Reflection;
using System.Windows.Forms;
namespace TrioVision.RBDMS.RedBoxAnalyser.InstallDB
{
[RunInstaller(true)]
public partial class
DataBaseInstaller : Installer
{
public DataBaseInstaller()
{
InitializeComponent();
}
//重写虚构函数,安装程序中会自动执行这个函数
public override void
Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
try
{
//得到当前安装的程序的路径
Assembly assembly =
Assembly.GetExecutingAssembly();
System.IO.FileInfo FileInfo =
new System.IO.FileInfo(assembly.Location);
string basePath =
FileInfo.DirectoryName;
// 数据库连接
string dbConn =
"server=localhost\MyServer;uid=sa;pwd=123456;database=master";
//
要附加到的数据库服务器中的数据库名称和库文件路径
string dbName = "TestDB";
string mdfPath =
string.Format("{0}\TestDB.MDF", basePath);
string ldfPath =
string.Format("{0}\TestDB.LDF", basePath);
//启动 sqlserver 服务(
MSSQL$MyServer实例 )
RunSqlServer();
AttachDataBase(dbConn,
dbName, mdfPath, ldfPath); //附加数据库
}
catch
(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 启动 sqlserver 服务的方法
///
public void RunSqlServer()
{
try
{
Process p = new
Process();
p.StartInfo.FileName =
"cmd.exe";
p.StartInfo.Arguments = "/c
"net start MSSQL$MyServer"
";
// 设置不显示窗口
p.StartInfo.WindowStyle =
ProcessWindowStyle.Hidden;
p.StartInfo.CreateNoWindow =
true;
p.StartInfo.UseShellExecute =
false;
// 关闭Shell的使用
p.StartInfo.RedirectStandardInput = true; //
重定向标准输入
p.StartInfo.RedirectStandardOutput = true; //
重定向标准输出
p.StartInfo.RedirectStandardError = true; //
重定向错误输出
p.Start();
// 执行
p.Close();
p.Dispose();
}
catch
(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 附加数据库的方法
///
/// 数据库服务器的连接字符串
/// 要附加的数据库名称
/// 要附加的主数据库文件路径
/// 要附加的数据库日志文件路径
private void AttachDataBase(string dbConn,
string dbName, string mdfPath, string ldfPath)
{
try
{
//string cmdStr =
string.Format("EXEC sp_detach_db @dbname = '{0}'",
dbName);//需要先将数据库分离出来
String cmdStr =
string.Format("EXEC sp_attach_db @dbname = '{0}', @filename1 =
'{1}', @filename2 = '{2}'",
dbName, mdfPath, ldfPath);
SqlConnection sqlConn = new
SqlConnection(dbConn);
SqlCommand sqlCmd = new
SqlCommand(cmdStr, sqlConn);
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
sqlConn.Close();
sqlConn.Dispose();
sqlCmd.Dispose();
}
catch
(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
<!-- 正文结束 -->