在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
public partial class JSWyglInstaller : Installer { public JSWyglInstaller() { InitializeComponent(); } #region 程序代码 private System.Data.SqlClient.SqlCommand Command; private System.Data.SqlClient.SqlTransaction Trans; private string DBName; private string ServerName; private string AdminName; private string AdminPwd; private string port; private string dir; public static string VirDirSchemaName = "IIsWebVirtualDir";
private string _target; private DirectoryEntry _iisServer; private ManagementScope _scope; private ConnectionOptions _connection; #endregion //连接数据库服务器到方法: private bool ConnectDatabase() catch(Exception e) } private void RollBack() #endregion //如果不能正确连接数据库服务器,请检查你的连接字符串,或者将连接字符串写入文件查看 private string GetSql(string Name) try Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." Name); StreamReader reader = new StreamReader(strm); return reader.ReadToEnd(); #endregion //可以将此需要执行的SQL脚本放在此文本中 #region ExecuteSql 执行SQL语句,参考自MSDN private void ExecuteSql(string DataBaseName, string sqlstring) } private void ExecuteSqlForTrans(string DataBaseName, string sqlstring) }
//创建数据库及数据库表: protected bool CreateDBAndTable(string DBName) try catch (Exception ex) return Restult; } #endregion
/// protected bool RestoreDB(string DBName) //MessageBox.Show("DBDir:" + dir); dir += @"DBInit\"; bool Restult = false; string MSQL = "RESTORE DATABASE " + DBName + " FROM DISK = N'" + dir + @"wygl.bak' " + " WITH MOVE 'TXFee_Data' TO N'" + @"c:\" + DBName + ".mdf', " + " MOVE 'TXFee_log' TO N'" + @"c:\" + DBName + ".ldf' "; try ExecuteSql("master", "USE MASTER IF EXISTS (SELECT NAME FROM SYSDATABASES WHERE NAME='" + DBName + "') DROP DATABASE " + DBName); } if (Command != null) // File.Delete(dir + @"wygl.bak"); //} //}
} #endregion //这里可以到注册表读取SQL Server的安装路径,把恢复后的数据库文件放到data目录地下。 #region WriteWebConfig 修改web.config的连接数据库的字符串 private bool WriteWebConfig() if (!FileInfo.Exists) throw new InstallException("未找到Web.Config文件 :" + this.Context.Parameters["targetdir"] + "/web.config"); } xmlDocument.Load(FileInfo.FullName); foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"]) // 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); 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); Node.Attributes.GetNamedItem("value").Value = String.Format("Provider=SQLOLEDB.1;Password={3};Persist Security Info=False;User ID={2};Initial Catalog={1};Data Source={0}", ServerName, DBName, AdminName, AdminPwd); FoundIt = true; if( !FondReportServer) return FoundIt; } //MessageBox.Show("webconfig"); if (!FileInfo.Exists) } xmlDocument.Load(FileInfo.FullName); // bool FoundIt = false, FondReportServer = false; foreach (System.Xml.XmlNode Node in xmlDocument["configuration"]["appSettings"]) string strNodeConnection= 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); }
private void WriteRegistryKey() RegistryKey hklm = Registry.LocalMachine; RegistryKey qzg = hklm.OpenSubKey("SOFTWARE", true);
RegistryKey F = qzg.CreateSubKey("QzgWygl");
//F.SetValue("FilePath", "kkkk"); old } #endregion
//操作IIS,建立网站等。可参考: //用VS2005制作网页对IIS进行操作 return false; try { _iisServer = new DirectoryEntry("IIS://" + iis + "/W3SVC/1"); _target = iis; _connection = new ConnectionOptions(); _scope = new ManagementScope(@"\\" + iis + @"\root\MicrosoftIISV2", _connection); _scope.Connect(); } catch { } return IsConnected(); }
public bool IsConnected() { if (_target == null || _connection == null || _scope == null) return false; return _scope.IsConnected; } #endregion
public bool IsWebSiteExists(string serverID) { try { string siteName = "W3SVC/" + serverID; ManagementObjectSearcher searcher = new ManagementObjectSearcher(_scope, new ObjectQuery("SELECT * FROM IIsWebServer"), null);
ManagementObjectCollection webSites = searcher.Get(); foreach (ManagementObject webSite in webSites) { if ((string)webSite.Properties["Name"].Value == siteName) return true; }
return false; } catch { return false; } } #endregion
private int GetNextOpenID() { DirectoryEntry iisComputer = new DirectoryEntry("IIS://localhost/w3svc"); int nextID = 0; foreach (DirectoryEntry iisWebServer in iisComputer.Children) string sname = iisWebServer.Name; try { int name = int.Parse(sname); if (name > nextID) { nextID = name; } } catch { } } return ++nextID; } #endregion
public string CreateWebSite(string serverID, string serverComment, string defaultVrootPath, string HostName, string IP, string Port) { try { ManagementObject oW3SVC = new ManagementObject(_scope, new ManagementPath(@"IIsWebService='W3SVC'"), null);
if (IsWebSiteExists(serverID)) { return "Site Already Exists..."; }
ManagementBaseObject inputParameters = oW3SVC.GetMethodParameters("CreateNewSite"); ManagementBaseObject[] serverBinding = new ManagementBaseObject[1]; serverBinding[0] = CreateServerBinding(HostName, IP, Port); inputParameters["ServerComment"] = serverComment; inputParameters["ServerBindings"] = serverBinding; inputParameters["PathOfRootVirtualDir"] = defaultVrootPath; inputParameters["ServerId"] = serverID;
ManagementBaseObject outParameter = null; outParameter = oW3SVC.InvokeMethod("CreateNewSite", inputParameters, null);
// 启动网站 string serverName = "W3SVC/" + serverID; ManagementObject webSite = new ManagementObject(_scope, new ManagementPath(@"IIsWebServer='" + serverName + "'"), null); webSite.InvokeMethod("Start", null);
return (string)outParameter.Properties["ReturnValue"].Value; } return ex.Message; } }
{ try { ManagementClass classBinding = new ManagementClass(_scope, new ManagementPath("ServerBinding"), null); ManagementObject serverBinding = classBinding.CreateInstance(); serverBinding.Properties["Hostname"].Value = HostName; serverBinding.Properties["IP"].Value = IP; serverBinding.Properties["Port"].Value = Port; serverBinding.Put(); return serverBinding; } catch { return null; } } #endregion //好了,准备工作已经做完,现在开始写最重要的Install方法了 //整个方法写完后如下: #region Install 安装 /// /// 安装数据库 /// /// public override void Install(IDictionary stateSaver) { dir = this.Context.Parameters["targetdir"]; DBName = this.Context.Parameters["dbname"].ToString(); AdminName = this.Context.Parameters["user"].ToString(); AdminPwd = this.Context.Parameters["pwd"].ToString(); iis = this.Context.Parameters["iis"].ToString(); port = this.Context.Parameters["port"].ToString(); //写入获取的安装程序中的变量,此段代码为调试用可以不添加
//}
string Urls = @"URL=http://"+iis+"/" + webdirs + @"/default.aspx"; File.Copy(strDeskTop + "\\亿联物业管理系统.url", startMenue + "\\亿联物业管理系统.url", true); string serverID = "5555"; string serverComment = "QZG"; string defaultVrootPath = this.Context.Parameters["targetdir"]; if (defaultVrootPath.EndsWith(@"\")) } string HostName = ""; string IP = ""; string Port = port; string sReturn = CreateWebSite(serverID, serverComment, defaultVrootPath, HostName, IP, Port); // 修改web.config //throw new ApplicationException("设置数据库连接字符串时出现错误"); } #endregion //删除时的方法。在本文中未详细操作,比如删除站点,删除数据库等。如果需要,请你自己补足 #region Uninstall 删除 //QZG } } #endregion //end |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论