標籤:enterprise library f# fsharp sql sqlite
使用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"#endifopen Systemopen System.Dataopen System.Data.Commonopen System.Data.SqlClientopen Microsoft.Practices.EnterpriseLibrary.Dataopen Microsoft.Practices.EnterpriseLibrary.Common.Configurationopen Microsoft.Practices.EnterpriseLibrary.Data.Sqlopen Microsoft.Practices.EnterpriseLibrary.Data.Configurationopen System.Threadingopen System.Threading.Tasksopen System.Configurationopen System.Data.SQLiteopen EntLibContrib.Data.SQLitelet 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
以上
通過fsharp探索Enterprise Library 6 DataBase 1.3 Sqlite