F#Type类中的SQL提供程序

我正在编写一个与Azure Worker角色一起使用的F#.我希望该类将连接字符串a作为参数.我创建了一个数据库连接
type dbSchema = sqlDataConnection<"...">
let db = dbSchema.GetDataContext()

但dbSchema是一个类型,所以它不能嵌入我的类(另一种类型).我可以创建两个单独的模块,一个使用db连接,另一个使用我的类

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来创建连接?

解决方法

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,longitudeRightBottom) =
    // Use local 'db' to access the database
    query { for v db.Table do select v }

与原始代码(模块中的db值)相比,这会为每个Factory创建一个新的db实例,但我想如果Factory将连接字符串作为参数,则会出现这种情况.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...