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

 

安装项目自定义过程:

image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值