如何解决IBatis.net与MySQL连接问题

 2022-10-27    464  

由于笔者最近在做一个跨数据库操作的测试,开始做IBatisNet (IBatis.DataMapper.1.6.2/IBatis.DataAccess.1.9.2)+ SQL Server2005的测试时,使用都很顺利,但是使用IBatisNet+ MySQL(5.1)的时候,却出现了一些问题。经过努力和网上收集终于解决了,下面把问题和解决办法一一贴出来共享。

问题描述:

如何解决IBatis.net与MySQL连接问题

1. 错误提示Check the MySQL。

出现这个问题首先请下载mySQL-connector-net,如果已经下载并安装了,继续检查IBatisNet的providers.config文件里的MySQL的provider节点的enabled是否设为“true”了,如果设为“true”,继续检查provider,官方提供的配置有些使用动态链接库文件版本很老,你需要手动去修改,例:

修改前:

<provider 

name="MySQL" 

description="MySQL,MySQLprovider1.0.7.30072" 

enabled="false" 

assemblyName="MySQL.Data, 
Version=1.0.7.30072,Culture=neutral, 
PublicKeyToken=c5687fc88969c44d"connectionClass="MySQL.Data.MySQLClient.MySQLConnection" 

commandClass="MySQL.Data.MySQLClient.MySQLCommand" 

parameterClass="MySQL.Data.MySQLClient.MySQLParameter" 

parameterDbTypeClass="MySQL.Data.MySQLClient.MySQLDbType" 

parameterDbTypeProperty="MySQLDbType" 

dataAdapterClass="MySQL.Data.MySQLClient.MySQLDataAdapter" 

commandBuilderClass="MySQL.Data.MySQLClient.MySQLCommandBuilder" 

usePositionalParameters="false" 

useParameterPrefixInSQL="true" 

useParameterPrefixInParameter="true" 

parameterPrefix="?" 

allowMARS="false" 

/> 

修改后(加亮这个一定要注意,如果为false,是不能连接数据库成功的):

<provider 

name="MySQL" 

description="MySQL, 
MySQLproviderV6.3.2.0" 

enabled="true" 

assemblyName="MySQL.Data,Version=6.3.2.0, 
Culture=neutral,PublicKeyToken=c5687fc88969c44d" 
connectionClass= 
"MySQL.Data.MySQLClient.MySQLConnection" 

commandClass= 
"MySQL.Data.MySQLClient.MySQLCommand" 

parameterClass= 
"MySQL.Data.MySQLClient.MySQLParameter" 

parameterDbTypeClass= 
"MySQL.Data.MySQLClient.MySQLDbType" 

parameterDbTypeProperty= 
"MySQLDbType" 

dataAdapterClass= 
"MySQL.Data.MySQLClient.MySQLDataAdapter" 

commandBuilderClass= 
"MySQL.Data.MySQLClient.MySQLCommandBuilder" 

usePositionalParameters="false" 

useParameterPrefixInSQL="true" 

useParameterPrefixInParameter="true" 

parameterPrefix="?" 

allowMARS="false" 

/> 

2. 错误提示 Unable to open connection to "MySQL, MySQL provider V6.3.2.0"。

出现这样的错误可能是Configure方法指定的配置文件的链接字符串有点问题,一下是我的,仅供参考。

<?xmlversion="1.0"encoding="utf-8"?> 

<SQLMapConfigxmlns= 
"http://ibatis.apache.org/dataMapper"xmlns:xsi= 
"http://www.w3.org/2001/XMLSchema-instance"> 

<!--以上为固定格式,从这里开始以下是用户配置项--> 

<settings> 

<!--该选项指示是否使用缓存,默认为true--> 

<settingcacheModelsEnabled="true"/> 

<!--当该选项为true时, 
你在调用指定的映射时你总是必须给出完整的名称例如: 
queryForObject(“Namespace.statement.Id”); 
--> 

<settinguseStatementNamespaces="false"/> 

</settings> 

<!--数据驱动提供类配置文件的路径和文件名--> 

<providersresource="providers.config"/> 

<!--指定一个属性源,相当与设置一些属性变量, 
该文件请查看下面--> 

<!--指定数据源${datasource}${database} 
${userid}${password} 
为DataBase.config定义的值--> 

<database> 

<!--<providername="ByteFx"></provider> 

<dataSourcename="IBatisNet" 
connectionString="Database=zy_test;DataSource= 
localhost;UserId=root;Password=1234"/>--> 

<providername="MySQL"></provider> 

<dataSourcename="IBatisNet" 
connectionString="Host=localhost;UserName= 
root;Password=1234;Database=zy_test;Port=3306; 
CharSet=utf8;AllowZeroDatetime=true"/> 

</database> 

<!--指定映射的文件的位置--> 

<SQLMaps> 

<!--从程序集中 

<SQLMapembedded="${root}Person.xml,${assembly}"/>--> 

<!--从文件中--> 

<SQLMapresource="SQLDetailXml/SystemXml/RoleInfo.xml"/> 

<SQLMapresource="SQLDetailXml/SystemXml/ModuleInfo.xml"/> 

<SQLMapresource="SQLDetailXml/SystemXml/UserInfo.xml"/> 

</SQLMaps> 

</SQLMapConfig> 

3. 错误提示 Character set ‘gbk’ is not supported。

出现此问题,可能你用了底版本的MySQL-connector-net,因为在MySQL Connector 1.07中提供的字符编码是有限的,找到不到web.config中设置的编码类型,没有gbk,换个版本高的MySQL Connector 。

  •  标签:  
  • MySQL
  •  

原文链接:https://77isp.com/post/4124.html

=========================================

https://77isp.com/ 为 “云服务器技术网” 唯一官方服务平台,请勿相信其他任何渠道。