在Godaddy的Windows主机上使用MySql.Data.dll连接数据库

来源:互联网 发布:淘宝买家举报卖家 编辑:程序博客网 时间:2024/05/18 03:04

由于Godaddy的Windows主机Deluxe方案只提供了2个200M的SQL Server 数据库,对于需要建立多个网站来说不是很方便。值得高兴的是,Godaddy提供了25个1G容量的MySQL数据库。而目前许多网站,或者自己建站都会考虑对不同的数据库的支持,BlogEngine.NET就是很好的例子(它几乎支持任何数据库,同时提供了XML作为数据源的支持),我们在编写网站的时候,也经常会使用ASP.NET中的System.Data.Common提供的工厂模式来连接数据库。

错误详情

当我们在本机上通过MySql.Data.dll能够正常运行网站,上传到Godaddy就会出现权限问题,提示如下错误:

Request for the permission of type 'System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

解决方案 1

遇到问题后,当然是到网上搜索解决方案,有意思的是一篇文章《MySQL vs. GoDaddy: The ASP.Net Work-Around》里说道Godaddy不让你轻松使用MySQL的,毕竟MySql.Data中部分功能会涉及到High Trust Level,而Godaddy限制了Trust Level为中级(Medium),所以通过MySql.Data来连接数据库就会报权限错误。

当然,知道了缘由,自然就有解决方案了。之前提到的那篇文章给出了一个比较有技术含量的方法,即重新编译MySql.Data.dll,让其能够在Medium trust environment下正常工作,并提供了一个已经编译好可以直接使用的MySql.Data.dll的文件供大家下载。虽然可行,但是不建议使用这个方案。

这个解决方法有很大的局限性,重新编译的MySql.Data.dll丧失了许多功能特性,同时也失去了对数据工厂的支持。我们只能在程序中显式指定前缀为MySql的数据库操作类来操作数据库(如MySqlConnection,MySqlCommand等等,下面代码显示了这样的局限性)

DataTable table = new DataTable();string connectionString = "******";using (MySqlConnection connection = new MySqlConnection(connectionString)){    connection.Open();    MySqlCommand command = connection.CreateCommand();    command.CommandText = "******";    MySqlDataAdapter adapter = new MySqlDataAdapter();    adapter.SelectCommand = command;    adapter.Fill(table);}

一旦我们的WEB程序没有指定使用的数据库,而是在运行时由工厂模式从web.config读取connectionString中的providerName来确定使用的数据库,则无能为力了。如下的代码使用了工厂模式来实现数据库操作

DataTable table = new DataTable();ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings["BaikeConn"];string connectString = connectionSetting.ConnectionString;string providerName = connectionSetting.ProviderName;DbProviderFactory dbFactory = DbProviderFactories.GetFactory(providerName);using (DbConnection connection = dbFactory.CreateConnection()){    connection.Open();    DbCommand command = connection.CreateCommand();    command.CommandText = "******";    DbDataAdapter adapter  = dbFactory.CreateDataAdapter();    adapter.SelectCommand = command;    adapter.Fill(table);}

其中Web.config的ConnectionString设置如下

<connectionStrings>  <add name="myConn" connectionString="user id=myUserid;password=myPassword;Database=myDatabase;Server=localhost;Connect Timeout=30" providerName="MySql.Data.MySqlClient"/></connectionStrings>

解决方案 2

这里给出一个更好的解决方案,完全没有必要重新编译MySql.Data.dll,而是换回较早版本的MySql.Connector.NET,比如6.0.4和6.0.7版本。它们在使用的过程中不会有权限问题,加上是官方原版,所以功能也不会有任何缺失。点击<<-这里->>下载6.0.7版本的MySql.Data.dll,将这个文件拷贝至网站的BIN目录。

使用的时候需要在web.config中加上如下一段,清除已有的数据库Provider,使用我们自己提供的MySql.Data.dll(因为Godaddy的服务器上已经安装了更老的并且有权限问题的MySql.Connector.NET,有点狠吧……感谢很好很强大的web.config,几乎任何配置都能改)

<system.data>    
  <
DbProviderFactories>
    <
clear/>
    <
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.0.7.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
  </
DbProviderFactories>
</
system.data>

其他的配置就没有任何不同了,尽情在Godaddy上使用MySQL吧。

 

http://www.wtnzone.com/post/2011/02/20/Use-MySql-Data-dll-on-Godaddy.aspx

原创粉丝点击