在C#项目中,很多时候到要用到Enterprise Library。这里只是用一个很简单的小例子来演示一下Enterprise Library在VS2010中操作mysql数据库的流程。
1,利用Enterprise Library操作mysql数据库。首先要具备一下天剑
(1)项目中要引用MySql.Data和MysroSoft.Practices.EnterpriseLibrary.Data这两个动态库。如果该项目的目标框架为.NET Framework 4 Client Profile,在程序编译过程中会报错,此时要把项目的目标框架改为.NET Framework 4(具体做法为:选中项目,点击右键,选择属性,然后进入修改即可)
(2)安装“mysql-connector-net-6.7.4.msi”。运行程序的环境中要安装“mysql-connector-net-6.7.4.msi”。他的版本要与(1)中MySql.Data的版本一致。
(3)在该项目的应用程序配置文件(app.config或web.config)中进行如下配置(配置中的版本号与上面的版本号一直).
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings,Microsoft.Practices.EnterpriseLibrary.Data"/>
</configSections>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"/>
<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.7.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"/>
</DbProviderFactories>
</system.data>
<connectionStrings>
<clear/>
<add name="mysql" connectionString="Database='test';Data Source='localhost';Port='3306';User Id='root';Password='';pooling=true"
providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
</configuration>
(4)基本操作的演示函数如下(所用数据库中含有一个名为table1的表。表中只有id和name两列):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;
namespace EnterpriseLibra