C#部署数据库及IIS站点
更新时间:2020年6月25日 11:16 点击:1457
一、前言
最近忙里偷闲,做了一个部署数据库及IIS网站站点的WPF应用程序工具。
二、内容
此工具的目的是:
- 根据.sql文件在本机上部署数据库
- 在本机部署IIS站点,包括新建站点,新建应用程序池。只新建而不会对本机上原有的程序池或站点做修改操作
最终样式:(Check按钮的作用是防止与本机已有的站点或程序池有冲突)
View:
<Window x:Class="AutoWebTool.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" xmlns:local="clr-namespace:AutoWebTool" Title="Web Site Automatic Deployment" Height="500" Width="800" WindowStartupLocation="CenterScreen" ResizeMode="NoResize"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="0.5*"/> <RowDefinition Height="0.5*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <GroupBox Header="DataBase Configuration" FontSize="15" BorderThickness="3" Margin="5,10" Grid.Row="0"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="65*"/> <ColumnDefinition Width="133*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="Server Address" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="1" Grid.Column="0" Text="User" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="2" Grid.Column="0" Text="Password" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="3" Grid.Column="0" Text="Script Path" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ServerAddress, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding User, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <PasswordBox Grid.Row="2" Grid.Column="1" PasswordChar="*" local:PasswordBoxHelper.Password="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32"> <i:Interaction.Behaviors> <local:PasswordBoxBehavior /> </i:Interaction.Behaviors> </PasswordBox> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding SqlPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <Button Grid.Row="4" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Browse" Click="FilePathBrowse_Click"/> </Grid> </GroupBox> <GroupBox Header="WebSite And Pool" FontSize="15" BorderThickness="3" Margin="5,10" Grid.Row="1"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="65*"/> <ColumnDefinition Width="133*"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Text="WebSite Name" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="1" Grid.Column="0" Text="WebSite ID" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="2" Grid.Column="0" Text="WebSite PhysicalPath" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="3" Grid.Column="0" Text="WebSite Port" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26" /> <TextBlock Grid.Row="4" Grid.Column="0" Text="Application Pool Name" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15" Height="26"/> <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding WebSiteName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding WebSiteID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding PhysicalPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding WebSitePort, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding PoolName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" VerticalAlignment="Center" FontSize="15" Width="450" Height="32" /> <Button Grid.Row="0" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Check" Click="WebSiteNameCheck_Click"/> <Button Grid.Row="1" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Check" Click="WebSiteIDCheck_Click"/> <Button Grid.Row="2" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Browse" Click="PathBrowse_Click"/> <Button Grid.Row="3" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Check" Click="WebSitePortCheck_Click"/> <Button Grid.Row="4" Grid.Column="2" Width="70" Height="25" Margin="0,0,10,0" HorizontalAlignment="Left" VerticalAlignment="Center" Content="Check" Click="PoolNameCheck_Click"/> </Grid> </GroupBox> <StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="10"> <Button Width="70" Height="25" Content="OK" Click="Deploy_Click"/> <Button Width="70" Height="25" Content="Cancel" Margin="10,0,0,0" Click="Close_Click"/> </StackPanel> </Grid> </Window>
View的后台文件:
using System.Windows; using System.Windows.Controls; using System.Windows.Interactivity; namespace AutoWebTool { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private AutoGenerationVM _vm; public MainWindow() { InitializeComponent(); DataContext = new AutoGenerationVM(); _vm = DataContext as AutoGenerationVM; } private bool sqlPath; private void FilePathBrowse_Click(object sender, RoutedEventArgs e) { sqlPath = _vm.GetSqlFilePath(); } private void WebSiteNameCheck_Click(object sender, RoutedEventArgs e) { var isInUse = _vm.CheckNameAndID(); if (isInUse) { MessageBox.Show("1.This name is Empty \r\n2.This name is in use,please change name!"); } else { MessageBox.Show("No Problem!"); } } private void WebSiteIDCheck_Click(object sender, RoutedEventArgs e) { var isInUse = _vm.CheckNameAndID(); if (isInUse) { MessageBox.Show("1.This ID is Empty \r\n2.This ID is in use,please change ID!"); } else { MessageBox.Show("No Problem!"); } } private bool physicalPath; private void PathBrowse_Click(object sender, RoutedEventArgs e) { physicalPath = _vm.GetFolderPath(); } private void WebSitePortCheck_Click(object sender, RoutedEventArgs e) { var isInUse = _vm.CheckWebPort(); if (isInUse) { MessageBox.Show("1.This port is Empty \r\n2.This port is in use,please change port!"); } else { MessageBox.Show("No Problem!"); } } private void PoolNameCheck_Click(object sender, RoutedEventArgs e) { var isInUse = _vm.CkeckPoolName(); if (isInUse) { MessageBox.Show("1.This pool name is Empty \r\n2.This name is in use,please change name!"); } else { MessageBox.Show("No Problem!"); } } private void Deploy_Click(object sender, RoutedEventArgs e) { var dataBaseServerAddressChecked = string.IsNullOrEmpty(_vm.ServerAddress); var dataBaseUserChecked = string.IsNullOrEmpty(_vm.User); var dataBasePasswordChecked = string.IsNullOrEmpty(_vm.Password); var dataBaseScriptChecked = sqlPath; var dataBaseCondition = !dataBaseServerAddressChecked && !dataBaseUserChecked && !dataBasePasswordChecked && !dataBaseScriptChecked; var webSiteNameAndIDChecked = _vm.CheckNameAndID(); var webSitePortChecked = _vm.CheckWebPort(); var applicationPoolNameChecked = _vm.CkeckPoolName(); var webSiteCondition = !webSiteNameAndIDChecked && !physicalPath && !webSitePortChecked && !applicationPoolNameChecked; if (dataBaseCondition&& webSiteCondition) { _vm.Execute(); } else { MessageBox.Show("Please Check Your Input!"); } } private void Close_Click(object sender, RoutedEventArgs e) { Close(); } } public static class PasswordBoxHelper { public static readonly DependencyProperty PasswordProperty = DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordBoxHelper), new FrameworkPropertyMetadata(string.Empty, OnPasswordPropertyChanged)); private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) { var passwordBox = sender as PasswordBox; string password = (string)e.NewValue; if (passwordBox != null && passwordBox.Password != password) { passwordBox.Password = password; } } public static string GetPassword(DependencyObject dp) { return (string)dp.GetValue(PasswordProperty); } public static void SetPassword(DependencyObject dp, string value) { dp.SetValue(PasswordProperty, value); } } public class PasswordBoxBehavior : Behavior<PasswordBox> { protected override void OnAttached() { base.OnAttached(); AssociatedObject.PasswordChanged += OnPasswordChanged; } private static void OnPasswordChanged(object sender, RoutedEventArgs e) { var passwordBox = sender as PasswordBox; string password = PasswordBoxHelper.GetPassword(passwordBox); if (passwordBox != null && passwordBox.Password != password) { PasswordBoxHelper.SetPassword(passwordBox, passwordBox.Password); } } protected override void OnDetaching() { base.OnDetaching(); AssociatedObject.PasswordChanged -= OnPasswordChanged; } } }
ViewModel:
using System; using System.DirectoryServices; using System.ComponentModel; using System.Linq; using System.Net; using System.Net.NetworkInformation; using Microsoft.Web.Administration; using System.Windows.Forms; using System.Diagnostics; using System.Data.SqlClient; using System.IO; namespace AutoWebTool { public class AutoGenerationVM : INotifyPropertyChanged { public AutoGenerationVM() { _physicalPath = AppDomain.CurrentDomain.BaseDirectory; } //DataBase ServerAddress private string _serverAddress = string.Empty; public string ServerAddress { get { return _serverAddress; } set { if (_serverAddress != value) { _serverAddress = value; NotifyPropertyChanged("ServerAddress"); } } } //DataBase User private string _user = string.Empty; public string User { get { return _user; } set { if (_user != value) { _user = value; NotifyPropertyChanged("User"); } } } //DataBase Password private string _password = string.Empty; public string Password { get { return _password; } set { if (_password != value) { _password = value; NotifyPropertyChanged("Password"); } } } //DataBase SQLPath private string _sqlPath = string.Empty; public string SqlPath { get { return _sqlPath; } set { if (_sqlPath != value) { _sqlPath = value; NotifyPropertyChanged("SqlPath"); } } } public bool GetSqlFilePath() { var openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "数据库脚本文件|*.sql"; if (openFileDialog.ShowDialog() == DialogResult.OK) { SqlPath = openFileDialog.FileName; } return false; } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //WebSite Name private string _webSiteName = string.Empty; public string WebSiteName { get { return _webSiteName; } set { if (_webSiteName != value) { _webSiteName = value; NotifyPropertyChanged("WebSiteName"); } } } //WebSite ID private string _webSiteID = string.Empty; public string WebSiteID { get { return _webSiteID; } set { if (_webSiteID != value) { _webSiteID = value; NotifyPropertyChanged("WebSiteID"); } } } /// <summary> /// Check WebSite Name and ID /// </summary> /// <returns></returns> public bool CheckNameAndID() { if (string.IsNullOrEmpty(WebSiteName) || string.IsNullOrEmpty(WebSiteID)) return true; DirectoryEntry rootEntry = new DirectoryEntry("IIS://localhost/w3svc"); foreach (DirectoryEntry entry in rootEntry.Children) { if (entry.SchemaClassName.Equals("IIsWebServer", StringComparison.OrdinalIgnoreCase)) { if (WebSiteID == entry.Name) { return true; } if (entry.Properties["ServerComment"].Value.ToString() == WebSiteName) { return true; } } } return false; } //Physical Path private string _physicalPath = string.Empty; public string PhysicalPath { get { return _physicalPath; } set { if (_physicalPath != value) { _physicalPath = value; NotifyPropertyChanged("PhysicalPath"); } } } /// <summary> /// Get Path for WebSite /// </summary> public bool GetFolderPath() { if (string.IsNullOrEmpty(PhysicalPath)) return true; var openFolderDialog = new FolderBrowserDialog(); if (openFolderDialog.ShowDialog() == DialogResult.OK) { PhysicalPath = openFolderDialog.SelectedPath; } return false; } //WebSite Port private string _webSitePort = string.Empty; public string WebSitePort { get { return _webSitePort; } set { if (_webSitePort != value) { _webSitePort = value; NotifyPropertyChanged("WebSitePort"); } } } /// <summary> /// Check WebSite Port /// </summary> /// <returns></returns> public bool CheckWebPort() { try { IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties(); IPEndPoint[] ipEndPoints = ipProperties.GetActiveTcpListeners(); foreach (IPEndPoint endPoint in ipEndPoints) { if (endPoint.Port == Convert.ToInt32(WebSitePort)) { return true; } } return false; } catch { return true; } } //Pool Name private string _poolName = string.Empty; public string PoolName { get { return _poolName; } set { if (_poolName != value) { _poolName = value; NotifyPropertyChanged("PoolName"); } } } /// <summary> /// Check Application Pool Name /// </summary> /// <returns></returns> public bool CkeckPoolName() { if (string.IsNullOrEmpty(PoolName)) return true; var manager = new ServerManager(); var list = manager.ApplicationPools; var matchedItem = list.FirstOrDefault(x => x.Name == PoolName); if (matchedItem != null) return true; return false; } /// <summary> /// Execute Script /// </summary> public void Execute() { //Deploy DataBase var tmpConn = new SqlConnection(); tmpConn.ConnectionString = "SERVER = " + ServerAddress +"; DATABASE = master; User ID = " + User+ "; Pwd = " + Password+ ";"; var scriptFile = new FileInfo(SqlPath); var sqlCreateDBQuery = scriptFile.OpenText().ReadToEnd(); SqlCommand myCommand = new SqlCommand(sqlCreateDBQuery, tmpConn); try { tmpConn.Open(); myCommand.ExecuteNonQuery(); MessageBox.Show("Database has been created successfully!","Create Database", MessageBoxButtons.OK,MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } finally { tmpConn.Close(); } try { //Deploy WebSite and Application Pool var script = "net start w3svc " + "& cd c:/Windows/System32/inetsrv " + "& appcmd add site /name:" + WebSiteName + " /id:" + WebSiteID + " /physicalPath:" + PhysicalPath + " /bindings:http/*:" + WebSitePort + ":" + WebSiteName + " & appcmd add apppool /name:" + PoolName + " /managedRuntimeVersion:v4.0 /managedPipelineMode:Integrated" + " & appcmd set site /site.name:" + WebSiteName + " /[path='/'].applicationPool:" + PoolName; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.WorkingDirectory = @"C:\Windows\System32"; startInfo.FileName = @"C:\Windows\System32\cmd.exe"; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; startInfo.Verb = "RunAs"; Process process = new Process(); process.StartInfo = startInfo; process.Start(); process.StandardInput.WriteLine(script); process.StandardInput.WriteLine("&exit"); process.StandardInput.Flush(); process.StandardInput.Close(); process.WaitForExit(); MessageBox.Show("IIS WebSite and Application Pool Deployed Successfully!", "Create WebSite and Application Pool", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Information); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(string name) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
相关文章
- 我们在使用C#做项目的时候,基本上都需要制作登录界面,那么今天我们就来一步步看看,如果简单的实现登录界面呢,本文给出2个例子,由简入难,希望大家能够喜欢。...2020-06-25
- 这篇文章主要介绍了C# 字段和属性的的相关资料,文中示例代码非常详细,供大家参考和学习,感兴趣的朋友可以了解下...2020-11-03
- 操作类就是把一些常用的一系列的数据库或相关操作写在一个类中,这样调用时我们只要调用类文件,如果要执行相关操作就直接调用类文件中的方法函数就可以实现了,下面整理了...2016-11-25
- 这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
- 这篇文章主要介绍了C#实现简单的Http请求的方法,以实例形式较为详细的分析了C#实现Http请求的具体方法,需要的朋友可以参考下...2020-06-25
- 本文给大家分享C#连接SQL数据库和查询数据功能的操作技巧,本文通过图文并茂的形式给大家介绍的非常详细,需要的朋友参考下吧...2021-05-17
- 本文主要介绍了C#中new的几种用法,具有很好的参考价值,下面跟着小编一起来看下吧...2020-06-25
使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序)
这篇文章主要介绍了使用Visual Studio2019创建C#项目(窗体应用程序、控制台应用程序、Web应用程序),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25- 这篇文章主要介绍了C#开发Windows窗体应用程序的简单操作步骤,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-12
- 这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
- 最近做一个小项目不可避免的需要前端脚本与后台进行交互。由于是在asp.net中实现,故问题演化成asp.net中jiavascript与后台c#如何进行交互。...2020-06-25
- 本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
- 轻松学习C#的基础入门,了解C#最基本的知识点,C#是一种简洁的,类型安全的一种完全面向对象的开发语言,是Microsoft专门基于.NET Framework平台开发的而量身定做的高级程序设计语言,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C#变量命名规则小结,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-09
- 这篇文章主要介绍了C#绘制曲线图的方法,以完整实例形式较为详细的分析了C#进行曲线绘制的具体步骤与相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 这篇文章主要介绍了c#自带缓存使用方法,包括获取数据缓存、设置数据缓存、移除指定数据缓存等方法,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了c#中(&&,||)与(&,|)的区别详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要用实例讲解C#递归算法的概念以及用法,文中代码非常详细,帮助大家更好的参考和学习,感兴趣的朋友可以了解下...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25