1、引用dll 安裝後在安裝目錄下找到以下dll: 先復制到項目目錄先,並引用。 2、配置連接數據庫的配置文件 你可以利用程序配置,也可以生成一個空白文檔手動配置。如圖, 第一步,運行安裝目錄下EntLibConfig.exe: 第二步,選擇blocks菜單配置數據庫連接: 第三步,配置連接數據庫,也可以直接File-save as保存為一個空config文件手動配置: 這裡是保存一個空的配置進行手動配置,空配置如下: [html] <configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> <dataConfiguration defaultDatabase="Connection String" /> <connectionStrings> <add name="Connection String" connectionString="Database=Database;Server=(local)\SQLEXPRESS;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration> <configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> <dataConfiguration defaultDatabase="Connection String" /> <connectionStrings> <add name="Connection String" connectionString="Database=Database;Server=(local)\SQLEXPRESS;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>手動配置,這裡以oracle為例進行配置。如下: [html] <?xml version="1.0"?> <configuration> <configSections> <!--添加配置企業庫(1)--> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> <!--添加配置企業庫(2)--> <dataConfiguration defaultDatabase="Oracle.ConnectionString"/> <!--添加配置企業庫(3)--> <connectionStrings> <add name="Oracle.ConnectionString" connectionString="Data Source=數據庫名;Persist Security Info=True; User ID=用戶名;Password=密碼;Unicode=True" providerName="System.Data.OracleClient"/> </connectionStrings> </configuration> <?xml version="1.0"?> <configuration> <configSections> <!--添加配置企業庫(1)--> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> </configSections> <!--添加配置企業庫(2)--> <dataConfiguration defaultDatabase="Oracle.ConnectionString"/> <!--添加配置企業庫(3)--> <connectionStrings> <add name="Oracle.ConnectionString" connectionString="Data Source=數據庫名;Persist Security Info=True; User ID=用戶名;Password=密碼;Unicode=True" providerName="System.Data.OracleClient"/> </connectionStrings> </configuration> 好了,這些准備工作完成了,下面看代碼中如何使用吧。這裡測試讀取,當然其他的操作都一樣。 [csharp] public class OracleSqlHelper { private static Database db = null; public static DataSet GetDataTable( string commandText) { try { DataSet resultDataSet = null; db = DatabaseFactory.CreateDatabase();//建立數據庫連接 resultDataSet =db.ExecuteDataSet(CommandType.Text, commandText);//執行 return resultDataSet; } catch (Exception ex) { return null; throw ex; } } } public class OracleDataProvider { public static DataTable TestReadTable(string id) { DataTable dataTable = null; try { string commandText = "select tu.name from tabUser tu"; DataSet resultTable = OracleSqlHelper.GetDataTable(commandText); int table = resultTable != null ? resultTable.Tables.Count : 0; if (table > 0) { dataTable = resultTable.Tables[0]; } } catch (Exception e) { throw e; } return dataTable; } } public class OracleSqlHelper { private static Database db = null; public static DataSet GetDataTable( string commandText) { try { DataSet resultDataSet = null; db = DatabaseFactory.CreateDatabase();//建立數據庫連接 resultDataSet =db.ExecuteDataSet(CommandType.Text, commandText);//執行 return resultDataSet; } catch (Exception ex) { return null; throw ex; } } } public class OracleDataProvider { public static DataTable TestReadTable(string id) { DataTable dataTable = null; try { string commandText = "select tu.name from tabUser tu"; DataSet resultTable = OracleSqlHelper.GetDataTable(commandText); int table = resultTable != null ? resultTable.Tables.Count : 0; if (table > 0) { dataTable = resultTable.Tables[0]; } } catch (Exception e) { throw e; } return dataTable; } }調用 object result = OracleDataProvider.TestReadTable("110"); 當然這裡僅僅是企業庫進行連接數據庫的用法,所以可以將用到的dll復制單獨使用。如果針對大型應用的話,當然不會這麼簡單。應該安裝所有dll,並且使用EntLibConfig.exe在block下進行配置所需要的功能的配置文件。