加入收藏 | 设为首页 | 会员中心 | 我要投稿 湖南网 (https://www.hunanwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程 > 正文

F#Type类中的SQL提供措施

发布时间:2021-02-24 23:08:23 所属栏目:编程 来源:网络整理
导读:我正在编写一个与Azure Worker脚色一路行使的F#.我但愿该类将毗连字符串a作为参数.我建设了一个数据库毗连 type dbSchema = SqlDataConnection"..."let db = dbSchema.GetDataContext() 但dbSchema是一个范例,以是它不能嵌入我的类(另一种范例).我可以建设两

我正在编写一个与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将毗连字符串作为参数,则会呈现这种环境.

(编辑:湖南网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读