使用Enterprise Library就是为了尽可能少的开发常用组件。数据库在选择的过程中常会面临部署,版权,个人喜好等诸多考量。最佳的处理方法就是添加一层数据抽象层,再切换Enterprise的过程中进行无缝衔接。由于我的笔记本跑的越来越慢,又想尝试一下使用Sqlite所以,就用Sqlite做例子。
我用的是Enterprise Library 6.0原本以为只需要简单的配置就可以进行实验,没想到GitHub上Sqlite提供的Sqlite兼容组件到Enterprise Library 4.1后就不提供了。我用网上的配置文件进行实验,果然有不支持的类提示。原来总以为是没有提供正确的Database TypeProvider,试了很多次都没有结果。总是出错时最好的解决办法就是休息,然后到处看看。果然已经有高人在codeproject上试出来了。自己扩展了
类,以后再研究。先回到正题。
我在配置文件中用了Sqlite自带的测试数据库。通过发送sql语句查询数据库的代码几乎没有改动就成功的获取了希望的结果。
配置文件
<?xml version="1.0"?> <configuration> <configSections> <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" /> <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="true" /> </configSections> <dataConfiguration defaultDatabase="sss" > <providerMappings> <add databaseType="EntLibContrib.Data.SQLite.SQLiteDatabase, EntLibContrib.Data.SQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" name="System.Data.SQLite" /> </providerMappings> </dataConfiguration> <system.data> </system.data> <connectionStrings> <add name="ExampleDatabase" connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=E:\WorkHell\fsharp-practise\EnterpriseLibraryPractise\DataAccessExamples.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="AsyncExampleDatabase" connectionString="Data Source=(localdb)\v11.0;Asynchronous Processing=true;AttachDbFilename=|DataDirectory|\DataAccessExamples.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="DataAccessExample.Properties.Settings.DataAccessExamplesConnectionString" connectionString="Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|\DataAccessExamples.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> <add name="sss" connectionString="Data Source=E:\WorkHell\fsharp-practise\EnterpriseLibraryPractise\dbdemos.db3;Version=3;" providerName="System.Data.SQLite" /> </connectionStrings> </configuration>
代码
#if INTERACTIVE #I @"E:\WorkHell\fsharp-practise\packages" #r @"EnterpriseLibrary.Data.6.0.1304.0\lib\NET45\Microsoft.Practices.EnterpriseLibrary.Data.dll" #r @"EnterpriseLibrary.Common.6.0.1304.0\lib\NET45\Microsoft.Practices.EnterpriseLibrary.Common.dll" #r @"System.Data.SQLite.Core.1.0.94.0\lib\net45\System.Data.SQLite.dll" #r @"EntLibContrib.Data.SQLite.dll" #r "System" #r "System.Data" #r "System.Configuration" #endif open System open System.Data open System.Data.Common open System.Data.SqlClient open Microsoft.Practices.EnterpriseLibrary.Data open Microsoft.Practices.EnterpriseLibrary.Common.Configuration open Microsoft.Practices.EnterpriseLibrary.Data.Sql open Microsoft.Practices.EnterpriseLibrary.Data.Configuration open System.Threading open System.Threading.Tasks open System.Configuration open System.Data.SQLite open EntLibContrib.Data.SQLite let DisplayRowValue (reader:IDataReader) = while reader.Read() do for i = 0 to reader.FieldCount-1 do Console.WriteLine("{0}={1}", reader.GetName(i), reader.[i].ToString()) Console.WriteLine() let path = __SOURCE_DIRECTORY__ + "\FsiApp.config" let fileMap = ConfigurationFileMap(path) let config = ConfigurationManager.OpenMappedMachineConfiguration(fileMap) let factory = new DatabaseProviderFactory(fun s -> config.GetSection(s)) let defaultDB = factory.Create("sss") let reader = defaultDB.ExecuteReader(CommandType.Text, "select * from orders") DisplayRowValue reader
以上
时间: 2024-11-10 08:06:22