资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

C#部署数据库及IIS站点

一、前言

创新互联是一家专业提供和林格尔企业网站建设,专注与做网站、成都网站制作、H5高端网站建设、小程序制作等业务。10年已为和林格尔众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

最近忙里偷闲,做了一个部署数据库及IIS网站站点的WPF应用程序工具。 

二、内容

此工具的目的是:

  • 根据.sql文件在本机上部署数据库
  • 在本机部署IIS站点,包括新建站点,新建应用程序池。只新建而不会对本机上原有的程序池或站点做修改操作

最终样式:(Check按钮的作用是防止与本机已有的站点或程序池有冲突)

C#部署数据库及IIS站点

View:


  
    
      
      
      
    
    
      
        
          
          
          
          
        
        
          
          
          
        

        
        
        
        

        
        
        
          
            
          
        
        

        

View的后台文件:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace AutoWebTool
{
  /// 
  /// MainWindow.xaml 的交互逻辑
  /// 
  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
  {
    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");
        }
      }
    }


    /// 
    /// Check WebSite Name and ID
    /// 
    /// 
    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");
        }
      }
    }

    /// 
    /// Get Path for WebSite
    /// 
    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");
        }
      }
    }


    /// 
    /// Check WebSite Port
    /// 
    /// 
    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");
        }
      }
    }


    /// 
    /// Check Application Pool Name
    /// 
    /// 
    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;
    }


    /// 
    /// Execute Script
    /// 
    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#部署数据库及IIS站点
文章网址:http://cdkjz.cn/article/jsjpoj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220