2012-10-28 112 views
2

我在寫一個F#以與Azure Worker角色一起使用。我希望該類具有連接字符串a作爲參數。我創建F#類中的SQL類型提供程序

type dbSchema = SqlDataConnection<"..."> 
let db = dbSchema.GetDataContext() 

一個數據庫連接,但找到dbschema是一種類型,因此它不能在我的課(另一種類型)被包埋。我可以創建兩個獨立的模塊,一個與數據庫連接,另外一個我的同班同學

module DataSource = 

    [<Literal>] 
    let connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=Service;Integrated Security=True" 

    type dbSchema = SqlDataConnection<connectionString> 
    let db = dbSchema.GetDataContext() 

module DealerFactory = 

    type Factory(connectionString) = 

     member this.GetList(latitudeLeftTop, longitudeLeftTop, latitudeRightBottom, longitudeRightBottom) = 
     ".." 

但是我怎麼使用ConnectionString我在類的構造函數來創建連接?

回答

7

SQL數據庫的類型提供程序使用連接字符串用於兩個不同目的。首先,它需要一個(在編譯時)生成數據庫模式。其次,在實際運行程序時,您可以(可選)給它另一個在運行時使用。

編譯時連接字符串需要SqlDataConnection<...>被指定爲參數和運行時連接字符串可以被傳遞給GetDataContext(...)操作。

所以,你可以使用靜態已知編譯時連接字符串定義類型:

[<Literal>] 
let connectionString = "Data Source=.\SQLEXPRESS;Initial Catalog=Service; ..." 
type dbSchema = SqlDataConnection<connectionString> 

而且當你要創建的數據庫連接的情況下,你可以通過它的另一個連接字符串:

type Factory(connectionString) = 
    // Create connection to the DB using (a different) 
    // connection string specified at runtime 
    let db = dbSchema.GetDataContext(connectionString) 

    member this.GetList(latitudeLeftTop, longitudeLeftTop, 
         latitudeRightBottom, longitudeRightBottom) = 
    // Use local 'db' to access the database 
    query { for v db.Table do select v } 

與原密碼(與模塊在db值),是有區別的,這會爲每一個Factorydb實例,但我想這是預期,如果相比將連接字符串作爲參數。

+0

謝謝,我明白要生成架構,但由於某種原因,我的智能感知不提供帶參數的調用GetDataContext。但是當我這樣做時,編譯器不會抱怨。 –

+0

嗯,那很奇怪!它顯示在我的機器上的智能感知中。它在使用一些簡單的數據庫的簡單腳本文件中的行爲是否相同? (如果有一種方法來重新制作它,那麼提交一個bug是很好的...) –

相關問題