VS2005网站和SQL一起打包部署安装心得

最近临时搞了个办证中心的小东东(我现在公司就是搞办证中心网站及软件的),开发完了得找人过去安装或是把网站文件什么的打包发给对方安装。项目分网站文件及SQL数据两部门,也就是说在部署的时候得同时安装网站文件及在SQL数据库中建好相关表、视图等内容。
  我使用了VS2005中的安装和部署项目中的“Web安装项目”,大概使用了下发现部署网站相当方便,但部署SQL数据就得在原基础上进行自定义操作了。这东东以前没搞过,比较没有头绪,找了不少资料:
   带SQL数据库操作的安装部署视频教程
   视频教程带的原码(里面少个WEB项目,想研究的自己添加个WEB项目,然后里面随便加个页面就行了)
   视频教程带的PDF
   视频教程带的QA
  上面的教程看过后基本就对安装部署的自定义操作比较了解了,然后就可以按以上示例进行动手操作了。
  我在网上找到的比值得参考的一个全程教学: 如何用VS2005制作Web安装程序 -徐智雄的BLOG(附带: 用VS2005制作网页对IIS进行操作),这里写的更详细点,我这里也就偷偷懒不把全程写一回了,把一些值得注意的地方写下

   1、在绑定自定义安装对话框中参数时,如下图所示的地方:
uploads/200712/04_094522_1.gif

   注意:
  1. /DBServer="[DBSERVER]" /DBName="[DBNAME]" /UserName="[DBUSERNAME]" /Password="[DBPASSWORD]" /targetdir="[TARGETDIR]/"  
  每个参数之间得有 空格,/targetdir="[TARGETDIR]/"到时候得到的为当前安装路径字串并在最后面添加一个"/",当然也可以直接中/targetdir="[TARGETDIR]"。在写这些参数的时候推荐以 /DBServer="[DBSERVER]"这种格式,上面的教程都省略写成/DBServer=[DBSERVER]

   2、执行SQL语句时那个SQL.TXT要注意的地方
   注意:SQL.TXT(可以是其它文件名)的编码要为Unicode,文件中不要包含"GO",如果视图等内容要求必须是批查询中的第一条语句的SQL语句得用EXEC('语句内容')来处理,要不会报错的。如:
  1. exec('  
  2. CREATE TABLE [dbo].[py_Resume] (  
  3.   [R_ID] [int] IDENTITY (1, 1) NOT NULL ,  
  4.   [R_Content] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  5.   [R_DateTime] [datetime] NULL ,  
  6.   [C_ID] [int] NOT NULL   
  7. ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]  
  8. CREATE TABLE [dbo].[py_photo] (  
  9.   [PhotoID] [int] IDENTITY (1, 1) NOT NULL ,  
  10.   [WorkersID] [nvarchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  11.   [PhotoPath] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL   
  12. ) ON [PRIMARY]  
  13. ')  
   我折腾了不少时间才执行成功的SQL(大家好参考下)
  如果想在类似查询分析器那里比较方便的进行执行SQL的话可以参考以下代码段,这个我没试过,是调用osql.exe来执行SQL的,应该不会有像SqlCommand执行的问题了
  1. try   
  2. dim connStr As String = String.Format("data source={0};user id={1};password={2};persist security info=false;packet size=4096"Me.Context.Parameters.Item("server"), Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"))   
  3. '根据输入的数据库名称建立数据库   
  4. executesql(connstr, "master""CREATE DATABASE " + Me.Context.Parameters.Item("dbname"))   
  5. '调用osql执行脚本   
  6. dim sqlProcess As New System.Diagnostics.Process   
  7. sqlprocess.start   
  8. info.filename = "osql.exe "  
  9. sqlprocess.start   
  10. info.arguments = String.Format(" -U {0} -P {1} -d {2} -i {3}db.sql"Me.Context.Parameters.Item("user"), Me.Context.Parameters.Item("pwd"), Me.Context.Parameters.Item("dbname"), Me.Context.Parameters.Item("targetdir"))   
  11. sqlprocess.start   
  12. info.windowstyle = ProcessWindowStyle.Hidden   
  13. sqlprocess.start()   
  14. sqlprocess.waitforexit()    
  15. '等待执行   
  16. sqlprocess.close()   
  17. '删除脚本文件   
  18. dim sqlFileInfo As New System.IO.FileInfo(String.Format("{0}db.sql"Me.Context.Parameters.Item("targetdir")))   
  19. if sqlFileInfo.Exists Then  
  20. sqlfileinfo.delete()   
  21. end If  
  22. catch ex As Exception   
  23. throw ex   
  24. end Try   
   3、关于调试的问题
  这个问题我找了很久不知道怎么调,不过有个笨办法,使用try...catch的方式使用MessageBox把出错信息显示出来,我就是这么调的,折腾了一整天,呵呵。

   相关参考代码记录
  1. #region WriteWebConfig 修改web.config的连接数据库的字符串   
  2.   
  3.  private bool WriteWebConfig()   
  4.   
  5.  {   
  6.   
  7.  System.IO.FileInfo FileInfo = new System.IO.FileInfo(this.Context.Parameters["targetdir"] + "/web.config");   
  8.   
  9.  if (!FileInfo.Exists)   
  10.   
  11.  {   
  12.   
  13.  throw new InstallException("Missing config file :" + this.Context.Parameters["targetdir"] + "/web.config");   
  14.   
  15.  }   
  16.   
  17.     
  18.   
  19.  System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();   
  20.   
  21.  xmlDocument.Load(FileInfo.FullName);   
  22.   
  23.     
  24.   
  25.  bool FoundIt = false;   
  26.   
  27.  foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"])   
  28.   
  29.  {   
  30.   
  31.  if (Node.Name == "add")   
  32.   
  33.  {   
  34.   
  35.  if (Node.Attributes.GetNamedItem("key").Value == "ConnectionString")   
  36.   
  37.  {   
  38.   
  39.  Node.Attributes.GetNamedItem("value").Value = String.Format("Persist Security Info=False;Data Source={0};database={1};User ID={2};Password={3};Packet Size=4096;Pooling=true;Max Pool Size=100;Min Pool Size=1", ServerName, DBName, AdminName, AdminPwd);   
  40.   
  41.  FoundIt = true;   
  42.   
  43.  }   
  44.   
  45.  }   
  46.   
  47.  }   
  48.   
  49.     
  50.   
  51.  if (!FoundIt)   
  52.   
  53.  {   
  54.   
  55.  throw new InstallException("Error when writing the config file: web.config");   
  56.   
  57.  }   
  58.   
  59.     
  60.   
  61.  xmlDocument.Save(FileInfo.FullName);   
  62.   
  63.  return FoundIt;   
  64.   
  65.  }  
  66.  #endregion  
  67. #region WriteRegistryKey 写注册表。安装部署中,直接有一个注册表编辑器,可以在那里面设置。   
  68.   
  69.  private void WriteRegistryKey()   
  70.   
  71.  {   
  72.   
  73.  // 写注册表   
  74.   
  75.  RegistryKey hklm = Registry.LocalMachine;   
  76.   
  77.  RegistryKey cqfeng = hklm.OpenSubKey("SOFTWARE"true);   
  78.   
  79.     
  80.   
  81.  RegistryKey F = cqfeng.CreateSubKey("cqfeng");   
  82.   
  83.     
  84.   
  85.  F.SetValue("FilePath""kkkk");   
  86.   
  87.  }  
  88.  #endregion  
  89. #region Connect 连接IIS服务器   
  90.   
  91.  public bool Connect()   
  92.   
  93.  {   
  94.   
  95.     
  96.   
  97.  if (iis == null)   
  98.   
  99.  return false;   
  100.   
  101.  try  
  102.   
  103.  {   
  104.   
  105.  _iisServer = new DirectoryEntry("IIS:" + iis + "/W3SVC/1");   
  106.   
  107.  _target = iis;   
  108.   
  109.  _connection = new ConnectionOptions();   
  110.   
  111.  _scope = new ManagementScope(@"//" + iis + @"/root/MicrosoftIISV2", _connection);  
  112.  _scope.Connect();  
  113.  }  
  114.  catch  
  115.  {  
  116.    
  117. return false;  
  118.  }  
  119.  return IsConnected();  
  120.  }  
  121.    
  122.  public bool IsConnected()  
  123.  {  
  124.  if (_target == null || _connection == null || _scope == null) return false;  
  125.  return _scope.IsConnected;  
  126.  }  
  127.  #endregion  
  128.    
  129. #region IsWebSiteExists 判断网站是否已经存在  
  130.  public bool IsWebSiteExists(string serverID)  
  131.  {  
  132.  try  
  133.  {  
  134.  string siteName = "W3SVC/" + serverID;  
  135.  ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);  
  136.    
  137.  ManagementObjectCollection webSites = searcher.Get();  
  138.  foreach (ManagementObject webSite in webSites)  
  139.  {  
  140.  if ((string)webSite.Properties["Name"].Value == siteName)  
  141.  return true;  
  142.  }  
  143.    
  144.  return false;  
  145.  }  
  146.  catch  
  147.  {  
  148.  return false;  
  149.  }  
  150.  }  
  151.  #endregion  
  152.    
  153.  #region GetNextOpenID 获得一个新的ServerID  
  154.  private int GetNextOpenID()  
  155.  {  
  156.  DirectoryEntry iisComputer = new DirectoryEntry("IIS:localhost/w3svc");  
  157.  int nextID = 0;  
  158.  foreach (DirectoryEntry iisWebServer in iisComputer.Children)  
  159.  {  
  160.  string sname = iisWebServer.Name;  
  161.  try  
  162.  {  
  163.  int name = int.Parse(sname);  
  164.  if (name > nextID)  
  165.  {  
  166.  nextID = name;  
  167.  }  
  168.  }  
  169.  catch  
  170.  {  
  171.  }  
  172.  }  
  173.  return ++nextID;  
  174.  }  
  175.  #endregion  
  176.    
  177. #region CreateWebsite 添加网站  
  178.  public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)  
  179.  {  
  180.  try  
  181.  {  
  182.  ManagementObject oW3SVC = new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);  
  183.  if (IsWebSiteExists(serverID))  
  184.  {  
  185.  return "Site Already Exists...";  
  186.  }  
  187.  ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");  
  188.  ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];  
  189.  serverBinding[0] = CreateServerBinding(HostName, IP, Port);  
  190.  inputParameters["ServerComment"] = serverComment;  
  191.  inputParameters["ServerBindings"] = serverBinding;  
  192.  inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;  
  193.  inputParameters["ServerId"] = serverID;  
  194.    
  195.  ManagementBaseObject outParameter = null;  
  196.  outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);  
  197.  // 启动网站  
  198.  string serverName = "W3SVC/" + serverID;  
  199.  ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);  
  200.  webSite.InvokeMethod("Start", null);  
  201.  return (string)outParameter.Properties["ReturnValue"].Value;  
  202.  }  
  203.  catch (Exception ex)  
  204.  {  
  205.  return ex.Message;  
  206.  }  
  207.  }  
  208.  public ManagementObject CreateServerBinding(string HostName, string IP, string Port)  
  209.  {  
  210.  try  
  211.  {  
  212.  ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);  
  213.  ManagementObject serverBinding = classBinding.CreateInstance();  
  214.  serverBinding.Properties["Hostname"].Value = HostName;  
  215.  serverBinding.Properties["IP"].Value = IP;  
  216.  serverBinding.Properties["Port"].Value = Port;   
  217.   
  218.  serverBinding.Put();   
  219.   
  220.  return serverBinding;   
  221.   
  222.  }   
  223.   
  224.  catch  
  225.   
  226.  {   
  227.   
  228.  return null;   
  229.   
  230.  }   
  231.   
  232.  }  
  233.  #endregion  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值