zoukankan      html  css  js  c++  java
  • 如何添加".Net Framework Data Provider for MySQL"配置信息到目标主机中?

    在使用Entity Framework开发数据业务系统时,使用了MySQL数据库,ADO.NET driver for MySQL使用官网http://www.mysql.com/downloads/connector/net/中下载的提供程序,在开发环境中安装该提供程序后,该安装程序将修改系统配置文件“C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config”,修改后的文件段如下:

    隐藏行号 复制代码 这是一段程序代码。
    1. <system.data>
      
    2.   <DbProviderFactories>
      
    3.     <add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
    4.     <add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
    5.     <add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
    6.     <add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
      
    7.     <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
      
    8.   </DbProviderFactories>
      
    9. </system.data>
      

    配置节”<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"…”是安装程序添加的,默认主机环境中没有该配置节。

    当发布程序到目标主机时,没有该配置节时,应用程序将弹出异常,所以我们需要想办法修改目标主机上的配置将上面的配置节添加进去,可行的办法有一下几种:

    1、在可执行程序的配置文件中添加该配置段。

    此方法带来的后果是,在开发主机上调试程序变得非常不方便,因为该配置信息在系统中只能出现一次,如果配置文件加载配置是发现有两个相同段的配置(一个位于上文提到的machine.config中,一个位于你要调试的程序demo.exe.config中),程序将发生异常无法调试,有解决方法就是在调试时将该该配置段暂时注释,发布时取消注释,但是这样的反复操作太令人痛苦了,所以此方法建议不采用。

    2、程序启动时修改“machine.config”配置文件,添加配置段。

    此方法经验证部分可行,因为程序要修改该的配置文件存在于系统路径下,有文件保护,需要有授权才能访问并修改该文件,而授权又是相当繁琐的过程。所以此方法建议不采用。

    3、在安装程序中自定义安装过程,在该过程中修改“machine.config”配置文件。

    此方法屏蔽了方法2的授权过程,因为安装程序自身有权限修改任何文件,所以此方法为最佳方法。实现步骤如下,在主输出项目中添加一个继承自“Installer”类的子类,重载方法“ public override void Commit(IDictionary savedState)”,在该方法体内实现将配置节添加到“machine.config”配置文件中,然后在安装项目中自定义安装过程。

    自定义安装类:

    隐藏行号 复制代码 这是一段程序代码。
    1. using System;
      
    2. using System.Collections;
      
    3. using System.ComponentModel;
      
    4. using System.Configuration.Install;
      
    5. using System.IO;
      
    6. using System.Linq;
      
    7. using System.Reflection;
      
    8. using System.Xml.Linq;
      
    9. using System.Diagnostics;
      
    10. using System.Windows.Forms;
      
    11. namespace Freemansoft.Csm
      
    12. {
      
    13.     /// <summary>
      
    14.     /// 自定义安装。
      
    15.     /// </summary>
      
    16.     [RunInstaller(true)]
      
    17.     public partial class CsmInstaller : Installer
      
    18.     {
      
    19.         /// <summary>
      
    20.         /// 构造方法。
      
    21.         /// </summary>
      
    22.         public CsmInstaller()
      
    23.         {
      
    24.             InitializeComponent();
      
    25.         }
      
    26.         /// <summary>
      
    27.         /// 重载提交。
      
    28.         /// </summary>
      
    29.         public override void Commit(IDictionary savedState)
      
    30.         {
      
    31.             base.Commit(savedState);
      
    32.             CreateUnInstallBat();
      
    33.             CreateMySQLProviderSection();
      
    34.         }
      
    35.         /// <summary>
      
    36.         /// 创建“MySQL数据库”数据提供程序配置段。
      
    37.         /// </summary>
      
    38.         private static void CreateMySQLProviderSection()
      
    39.         {
      
    40.             string invarientValue = "MySql.Data.MySqlClient";
      
    41.             string machineCfgFileName = @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config";
      
    42.             string name = "MySQL Data Provider";
      
    43.             string description = ".Net Framework Data Provider for MySQL";
      
    44.             string type = "MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.2.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d";
      
    45.             XDocument document = XDocument.Load(machineCfgFileName);
      
    46.             if (document != null)
      
    47.             {
      
    48.                 XElement element = document.Root.Element("system.data");
      
    49.                 if (element != null)
      
    50.                 {
      
    51.                     element = element.Element("DbProviderFactories");
      
    52.                     if (element != null)
      
    53.                     {
      
    54.                         if (element.Elements().FirstOrDefault(a =>
      
    55.                             a.Attribute("invariant").Value == invarientValue) == null)
      
    56.                         {
      
    57.                             element.Add(new XElement("add",
      
    58.                                 new XAttribute("name", name),
      
    59.                                 new XAttribute("invariant", invarientValue),
      
    60.                                 new XAttribute("description", description),
      
    61.                                 new XAttribute("type", type)));
      
    62.                             document.Save(machineCfgFileName);
      
    63.                             Logging.Logger.Inform("Add the mysql data provider configuration to the " + machineCfgFileName);
      
    64.                         }
      
    65.                     }
      
    66.                 }
      
    67.             }
      
    68.         }
      
    69.         /// <summary>
      
    70.         /// 创建卸载批处理文件。
      
    71.         /// </summary>
      
    72.         protected void CreateUnInstallBat()
      
    73.         {
      
    74.             string dir = GetTargetDirectory();
      
    75.             FileStream fs = new FileStream(dir + "UnInstall.bat", FileMode.Create);
      
    76.             StreamWriter sw = new StreamWriter(fs);
      
    77.             sw.WriteLine("@echo off");
      
    78.             sw.WriteLine(@"C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe -u " + Path.Combine(GetTargetDirectory(), "DbSynchronization.exe"));
      
    79.             sw.WriteLine(string.Format("start /normal %windir%\\system32\\msiexec /x {0}", base.Context.Parameters["productCode"].ToString()));
      
    80.             sw.WriteLine("exit");
      
    81.             sw.Flush();
      
    82.             sw.Close();
      
    83.             fs.Close();
      
    84.         }
      
    85.         /// <summary>
      
    86.         /// 获取安装目标目录。
      
    87.         /// </summary>
      
    88.         protected string GetTargetDirectory()
      
    89.         {
      
    90.             string directory = Path.GetDirectoryName(base.Context.Parameters["assemblypath"].ToString());
      
    91.             if (directory[directory.Length - 1] != Path.DirectorySeparatorChar)
      
    92.             {
      
    93.                 directory += Path.DirectorySeparatorChar;
      
    94.             }
      
    95.             return directory;
      
    96.         }
      
    97.     }
      
    98. }
      

    安装项目自定义过程:

    image

  • 相关阅读:
    HDU 5818 Joint Stacks
    HDU 5816 Hearthstone
    HDU 5812 Distance
    HDU 5807 Keep In Touch
    HDU 5798 Stabilization
    HDU 5543 Pick The Sticks
    Light OJ 1393 Crazy Calendar (尼姆博弈)
    NEFU 2016省赛演练一 I题 (模拟题)
    NEFU 2016省赛演练一 F题 (高精度加法)
    NEFU 2016省赛演练一 B题(递推)
  • 原文地址:https://www.cnblogs.com/iamfreeman/p/2118738.html
Copyright © 2011-2022 走看看