设为首页 收藏本站
查看: 1023|回复: 0

[经验分享] No 105 · Web项目打包(自动创建iis和自动附加数据库)

[复制链接]

尚未签到

发表于 2015-11-14 15:25:40 | 显示全部楼层 |阅读模式
  1、  发布项目。
  2、  新建—》项目---》类库  取名:SetupClassLibrary
  右击类库—添加—安装程序类 MyInstaller   
  MyInstaller类有创建虚拟目录、附加数据库、修改webconfig等功能。在类中编写如下的代码:
  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections;
using System.Management;
using System.IO;
using System.DirectoryServices;
using System.Reflection;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Win32;
using System.Collections.Specialized;
using System.ServiceProcess;
using System.Windows.Forms;
namespace SetupClassLibrary
{
[RunInstaller(true)]
public partial class MyInstaller : Installer
{
//先设置私有成员,对应安装程序里接受到得用户输入
private string dbname;
private string dbserver;
private string user;
private string pwd;
private string iis;
private string physicaldir;
private string virtualdir;
private string rdovisit;
public static string VirDirSchemaName = "IIsWebVirtualDir";
private string _target;
private DirectoryEntry _iisServer;
private ManagementScope _scope;
private ConnectionOptions _connection;
public MyInstaller()
{
InitializeComponent();
}
#region WriteWebConfig 修改web.config的连接数据库的字符串 WriteWebConfig 修改web.config的连接数据库的字符串
private void WriteWebConfig()
{
//加载配置文件
System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/Web.config");
if (!FileInfo.Exists)
{
throw new InstallException("缺少配置文件 :" + this.Context.Parameters["targetdir"] + "/Web.config");
}
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(FileInfo.FullName);
//修改连接字符串
foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["connectionStrings"])
{
if (Node.Name == "add")
{
if (Node.Attributes.GetNamedItem("name").Value == "ConnectionString")
{
//if (rdovisit == "1")
//{
//    Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source=" + dbserver + ";Initial Catalog=" + dbname + ";Integrated Security=True");
//}
//else
//{
Node.Attributes.GetNamedItem("connectionString").Value = String.Format("Data Source=" + dbserver + ";Initial Catalog=" + dbname + ";User ID=" + user + ";pwd=" + pwd + ";");
// }
}
}
}
xmlDocument.Save(FileInfo.FullName);
}
#endregion
#region 创建虚拟目录 创建虚拟目录
private void CreateVirtualDir()
{
string constIISWebSiteRoot = "IIS://" + iis + "/W3SVC/1/ROOT";
DirectoryEntry root = new DirectoryEntry(constIISWebSiteRoot);
DirectoryEntry tbEntry = root.Children.Add(virtualdir, root.SchemaClassName);
tbEntry.Properties["Path"][0] = physicaldir;//设置物理地址
tbEntry.Invoke("AppCreate", true);
//tbEntry.Properties["DefaultDoc"][0] = "Default.aspx";//设置起始页
tbEntry.CommitChanges();
}
#endregion

#region Uninstall 删除 Uninstall 删除
public override void Uninstall(IDictionary savedState)
{
//添加自定义的卸载代码
if (savedState == null)
{
throw new ApplicationException("未能卸载!");
}
else
{
base.Uninstall(savedState);
}
}
#endregion
public override void Install(System.Collections.IDictionary mySavedState)
{
base.Install(mySavedState);
physicaldir = this.Context.Parameters["targetdir"].ToString();
virtualdir = this.Context.Parameters["virtualdir"].ToString();
dbname = this.Context.Parameters["dbname"].ToString();
dbserver = this.Context.Parameters["dbserver"].ToString();
user = this.Context.Parameters["user"].ToString();
rdovisit = this.Context.Parameters["rdovisit"].ToString();
pwd = this.Context.Parameters["pwd"].ToString();
iis = this.Context.Parameters["iis"].ToString();
string strSql = "";            
//if (rdovisit == "1")       //如果选中的是windows身份验证
//{
strSql = "Data Source=" + dbserver + ";Initial Catalog=master;User ID=" + user + ";pwd=" + pwd + ";";//连接数据库字符串
string strMdf = physicaldir + @"SharpRush.mdf";//MDF文件路径,这里需注意文件名要与刚添加的数据库文件名一样!
string strLdf = physicaldir + @"SharpRush.ldf";//LDF文件路径
this.CreateDataBase(strSql, dbname, strMdf, strLdf, physicaldir);//开始创建数据库
// 创建虚拟目录
CreateVirtualDir();
// 修改web.config
WriteWebConfig();
//}
//else  //如果选中的是sqlserver身份验证
//{
//strSql = "Data Source=" + dbserver + ";Initial Catalog=master;User ID=" + user + ";pwd=" + pwd + ";";//连接数据库字符串
//string strMdf = physicaldir + @"SharpRush.mdf";//MDF文件路径,这里需注意文件名要与刚添加的数据库文件名一样!
//string strLdf = physicaldir + @"SharpRush.ldf";//LDF文件路径
//this.CreateDataBase(strSql, dbname, strMdf, strLdf, physicaldir);//开始创建数据库
//// 创建虚拟目录
//CreateVirtualDir();
//// 修改web.config
//WriteWebConfig();
//}            
}
/// <summary>
/// 附加数据库方法
/// </summary>
/// <param name=&quot;strSql&quot;>连接数据库字符串,连接master系统数据库</param>
/// <param name=&quot;DataName&quot;>数据库名字</param>
/// <param name=&quot;strMdf&quot;>数据库文件MDF的路径</param>
/// <param name=&quot;strLdf&quot;>数据库文件LDF的路径</param>
/// <param name=&quot;path&quot;>安装目录</param>
private void CreateDataBase(string strSql, string DataName, string strMdf, string strLdf, string path)
{
SqlConnection myConn = new SqlConnection(strSql);
String str = null;
try
{
str = &quot; EXEC sp_attach_db @dbname='&quot; + DataName + &quot;',@filename1='&quot; + strMdf + &quot;',@filename2='&quot; + strLdf + &quot;'&quot;;
SqlCommand myCommand = new SqlCommand(str, myConn);
myConn.Open();
myCommand.ExecuteNonQuery();
MessageBox.Show(&quot;数据库安装成功!点击确定继续&quot;);//需Using System.Windows.Forms
}
catch (Exception e)
{
MessageBox.Show(&quot;数据库已存在,数据库安装失败!&quot; + e.Message + &quot;\n\n&quot; + &quot;请先分离数据库!&quot;);
System.Diagnostics.Process.Start(path);//打开安装目录
}
finally
{
myConn.Close();
}
}   
}
}

  
  3、右击项目解决方案—添加—新建项目
DSC0000.gif
  输入名称,选择存储路径。
DSC0001.gif
  右击”应用程序文件夹”---添加---项目输出
DSC0002.gif
  选择主输出,点击确定按钮。
  将项目发布出的文件复制到“应用程序文件夹”,同时复制数据库到该文件夹下。右击“应用程序文件夹”--添加—选择路径“C:\Program Files\Internet Explorer”下的“iexplore.exe”文件(该文件是ie应用程序),再添加 路径“C:\WINDOWS\system32”下的“msiexec.exe”文件。(该文件是卸载程序的应用文件)。
  然后右击“应用程序文件夹”下的“iexplore.exe” 文件和“msiexec.exe”文件,创建快捷方式并命名。将快捷方式拖到---用户的”程序”菜单文件夹下 ,再创建一个“iexplore.exe” 文件的快捷方式拖动到“用户菜单”文件夹下---右击快捷方式修改属性。其中“msiexec.exe”文件快捷方式的Arguments属性要修改为  /x produccode
  
  4、  右击安装项目---试图—用户界面
DSC0003.gif
  右击安装下的启动节点—添加对话框
  添加“文本框(A)”和“文本框(B)”
  右击修改两个文本框的属性
  文本框(A)
DSC0004.gif
DSC0005.gif
  

版权声明:本文为博主原创文章,未经博主允许不得转载。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-139245-1-1.html 上篇帖子: iis7.5+win2008 出现 HTTP Error 503. The service is unavailable. 下篇帖子: iis发布的网站用localhost可以访问,改成IP就无法访问的解决方案
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表