Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 如何在FSharp.Data.TypeProviders中使用app.config for db连接字符串?_F#_F# Data - Fatal编程技术网

F# 如何在FSharp.Data.TypeProviders中使用app.config for db连接字符串?

F# 如何在FSharp.Data.TypeProviders中使用app.config for db连接字符串?,f#,f#-data,F#,F# Data,我正在成功地使用FSharp.Data.TypeProviders访问我的数据库。我还使用FSharp.Configuration从我的app.config读取值 在F#中硬编码我的连接字符串是有问题的,因为我部署到了许多登台环境。因此,我希望将这两种方法结合起来,以便从app.config获取数据库连接字符串。不幸的是,我最初的尝试失败了 下面是一个摘要片段: " 开放系统 打开FSharp.Configuration 打开FSharp.Data.TypeProviders 类型设置=应用设置

我正在成功地使用
FSharp.Data.TypeProviders
访问我的数据库。我还使用
FSharp.Configuration
从我的app.config读取值

在F#中硬编码我的连接字符串是有问题的,因为我部署到了许多登台环境。因此,我希望将这两种方法结合起来,以便从app.config获取数据库连接字符串。不幸的是,我最初的尝试失败了

下面是一个摘要片段:

"

开放系统
打开FSharp.Configuration
打开FSharp.Data.TypeProviders
类型设置=应用设置
//在app.config中
让ConnectionString=Settings.ConnectionString.Target
类型Sql=SqlDataConnection
// ...
使用db=Sql.GetDataContext()
//查询数据库等。
"

我理解类型提供程序失败的原因,但我想问是否有人建议允许我动态提供连接字符串


澄清一下:我特别想使用FSharp.Data.TypeProviders,因为类型提供程序提供了一些功能。我不需要C#解决方案。

在生产环境中,您可能会遇到常规SQL数据提供程序的问题,因为编译机器需要与DB机器的实时连接。试试看,这可以解决这个问题,但也允许您以编程方式提供连接字符串

  • 首先运行
    sqlmetal
    将数据库架构信息提取到元语言文件中:从VS命令提示符运行
    sqlmetal/server:yourDB\SQLInstance/database:MyDB/sproc/dbml:schema.dbml
  • 将编译器指向该模式信息文件-无需再在编译时查询数据库:
    let dbml=DbmlFile
  • 然后,您可以在运行时提供实际的连接字符串:
    let db=dbml.MyDB(whateverConnectionString)

上还提供了一个很好的分步指南。连接字符串在编译后是固定的,这不是真的。谢谢,你说得对。更正。似乎我已经有了太多的“无变异”心态;-)
open System

open FSharp.Configuration             
open FSharp.Data.TypeProviders

type Settings = AppSettings<"app.config">

// in app.config <add name="Target"  connectionString="blahblah"/>
let ConnectionString = Settings.ConnectionStrings.Target

type Sql = SqlDataConnection<ConnectionString>

// ...
use db = Sql.GetDataContext()

// query db, etc.