VS2005网站和SQL一起打包部署安装心得【转载】

 

永久地址: http://blog.breakN.net/feed.asp?q=comment&id=397 
引用地址: http://blog.breakN.net/trackback.asp?id=397

[ 2007-12-04 09:23:43 | 作者: 景裔 ]

  本文原创,转载请注明出处
  最近临时搞了个办证中心的小东东(我现在公司就是搞办证中心网站及软件的),开发完了得找人过去安装或是把网站文件什么的打包发给对方安装。项目分网站文件及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.  
  9. CREATE TABLE [dbo].[py_photo] (  
  10.   [PhotoID] [int] IDENTITY (1, 1) NOT NULL ,  
  11.   [WorkersID] [nvarchar] (4) COLLATE Chinese_PRC_CI_AS NOT NULL ,  
  12.   [PhotoPath] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL   
  13. ) ON [PRIMARY]  
  14. ')  
   我折腾了不少时间才执行成功的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.  
  67.  #endregion  
  68.  
  69. #region WriteRegistryKey 写注册表。安装部署中,直接有一个注册表编辑器,可以在那里面设置。   
  70.   
  71.  private void WriteRegistryKey()   
  72.   
  73.  {   
  74.   
  75.  // 写注册表   
  76.   
  77.  RegistryKey hklm = Registry.LocalMachine;   
  78.   
  79.  RegistryKey cqfeng = hklm.OpenSubKey("SOFTWARE"true);   
  80.   
  81.     
  82.   
  83.  RegistryKey F = cqfeng.CreateSubKey("cqfeng");   
  84.   
  85.     
  86.   
  87.  F.SetValue("FilePath""kkkk");   
  88.   
  89.  }  
  90.  
  91.  #endregion  
  92. #region Connect 连接IIS服务器   
  93.   
  94.  public bool Connect()   
  95.   
  96.  {   
  97.   
  98.     
  99.   
  100.  if (iis == null)   
  101.   
  102.  return false;   
  103.   
  104.  try  
  105.   
  106.  {   
  107.   
  108.  _iisServer = new DirectoryEntry("IIS:" + iis + "/W3SVC/1");   
  109.   
  110.  _target = iis;   
  111.   
  112.  _connection = new ConnectionOptions();   
  113.   
  114.  _scope = new ManagementScope(@"\\" + iis + @"\root\MicrosoftIISV2", _connection);  
  115.  
  116.  _scope.Connect();  
  117.  
  118.  }  
  119.  
  120.  catch  
  121.  
  122.  {  
  123.  
  124.    
  125.  
  126. return false;  
  127.  
  128.  }  
  129.  
  130.  return IsConnected();  
  131.  
  132.  }  
  133.  
  134.    
  135.  
  136.  public bool IsConnected()  
  137.  
  138.  {  
  139.  
  140.  if (_target == null || _connection == null || _scope == null) return false;  
  141.  
  142.  return _scope.IsConnected;  
  143.  
  144.  }  
  145.  
  146.  #endregion  
  147.  
  148.    
  149.  
  150. #region IsWebSiteExists 判断网站是否已经存在  
  151.  
  152.  public bool IsWebSiteExists(string serverID)  
  153.  
  154.  {  
  155.  
  156.  try  
  157.  
  158.  {  
  159.  
  160.  string siteName = "W3SVC/" + serverID;  
  161.  
  162.  ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);  
  163.  
  164.    
  165.  
  166.  ManagementObjectCollection webSites = searcher.Get();  
  167.  
  168.  foreach (ManagementObject webSite in webSites)  
  169.  
  170.  {  
  171.  
  172.  if ((string)webSite.Properties["Name"].Value == siteName)  
  173.  
  174.  return true;  
  175.  
  176.  }  
  177.  
  178.    
  179.  
  180.  return false;  
  181.  
  182.  }  
  183.  
  184.  catch  
  185.  
  186.  {  
  187.  
  188.  return false;  
  189.  
  190.  }  
  191.  
  192.  }  
  193.  
  194.  #endregion  
  195.  
  196.    
  197.  
  198.  #region GetNextOpenID 获得一个新的ServerID  
  199.  
  200.  private int GetNextOpenID()  
  201.  
  202.  {  
  203.  
  204.  DirectoryEntry iisComputer = new DirectoryEntry("IIS:localhost/w3svc");  
  205.  
  206.  int nextID = 0;  
  207.  
  208.  foreach (DirectoryEntry iisWebServer in iisComputer.Children)  
  209.  
  210.  {  
  211.  
  212.  string sname = iisWebServer.Name;  
  213.  
  214.  try  
  215.  
  216.  {  
  217.  
  218.  int name = int.Parse(sname);  
  219.  
  220.  if (name > nextID)  
  221.  
  222.  {  
  223.  
  224.  nextID = name;  
  225.  
  226.  }  
  227.  
  228.  }  
  229.  
  230.  catch  
  231.  
  232.  {  
  233.  
  234.  }  
  235.  
  236.  }  
  237.  
  238.  return ++nextID;  
  239.  
  240.  }  
  241.  
  242.  #endregion  
  243.  
  244.    
  245.  
  246. #region CreateWebsite 添加网站  
  247.  
  248.  public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port)  
  249.  
  250.  {  
  251.  
  252.  try  
  253.  
  254.  {  
  255.  
  256.  ManagementObject oW3SVC = new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);  
  257.  
  258.  if (IsWebSiteExists(serverID))  
  259.  
  260.  {  
  261.  
  262.  return "Site Already Exists...";  
  263.  
  264.  }  
  265.  
  266.  ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite");  
  267.  
  268.  ManagementBaseObject[] serverBinding = new ManagementBaseObject[1];  
  269.  
  270.  serverBinding[0] = CreateServerBinding(HostName, IP, Port);  
  271.  
  272.  inputParameters["ServerComment"] = serverComment;  
  273.  
  274.  inputParameters["ServerBindings"] = serverBinding;  
  275.  
  276.  inputParameters["PathOfRootVirtualDir"] = defaultVrootPath;  
  277.  
  278.  inputParameters["ServerId"] = serverID;  
  279.  
  280.    
  281.  
  282.  ManagementBaseObject outParameter = null;  
  283.  
  284.  outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);  
  285.  
  286.  // 启动网站  
  287.  
  288.  string serverName = "W3SVC/" + serverID;  
  289.  
  290.  ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null);  
  291.  
  292.  webSite.InvokeMethod("Start", null);  
  293.  
  294.  return (string)outParameter.Properties["ReturnValue"].Value;  
  295.  
  296.  }  
  297.  
  298.  catch (Exception ex)  
  299.  
  300.  {  
  301.  
  302.  return ex.Message;  
  303.  
  304.  }  
  305.  
  306.  }  
  307.  
  308.  public ManagementObject CreateServerBinding(string HostName, string IP, string Port)  
  309.  
  310.  {  
  311.  
  312.  try  
  313.  
  314.  {  
  315.  
  316.  ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null);  
  317.  
  318.  ManagementObject serverBinding = classBinding.CreateInstance();  
  319.  
  320.  serverBinding.Properties["Hostname"].Value = HostName;  
  321.  
  322.  serverBinding.Properties["IP"].Value = IP;  
  323.  
  324.  serverBinding.Properties["Port"].Value = Port;   
  325.   
  326.  serverBinding.Put();   
  327.   
  328.  return serverBinding;   
  329.   
  330.  }   
  331.   
  332.  catch  
  333.   
  334.  {   
  335.   
  336.  return null;   
  337.   
  338.  }   
  339.   
  340.  }  
  341.  
  342.  #endregion  
Tag标签: vs打包 部署
posted on 2010-04-30 00:56  rex.ying  阅读(491)  评论(0编辑  收藏  举报