我正在使用FSharp.Data.SqlClient,并试图将connectionString从[<Literal>]移动到app.config。
我的app.config看起来像这样
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Test;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
</configuration>我的SqlCommandProvider如下所示,根据http://fsprojects.github.io/FSharp.Data.SqlClient/configuration%20and%20input.html的说法,这应该是正确的
new SqlCommandProvider<"SELECT ...",
ConnectionStringOrName = "name=DefaultConnection",
SingleRow = true,
AllParametersOptional = true>(??????)现在的问题是。最后一部分是什么,??????部分。
我尝试了"name=DefaultConnection",但是它给了我一个不支持名称的运行时错误。
我似乎找不到任何文件来解释那里发生了什么。
更新
解决了这个问题我找到了解决办法。https://fsprojects.github.io/FSharp.Configuration/
如果您必须提供连接字符串,我就不明白ConnectionStringOrName的用途了。另外,为什么你必须指定它两次。对我来说没有什么意义:
发布于 2016-11-02 11:56:10
当使用类型提供程序时,通常需要两个独立的数据源:
需要两个数据源的原因是,运行时数据源可能是在运行时确定的,而且在编译时可能无法访问它(通常您可以访问dev数据库,但不能访问生产数据库)。编译时连接必须是常量,这样提供者就可以(在编译代码时)使用它,而无需运行代码的任何部分。
如果使用SQL命令提供程序,则:
type SelectCmd = SqlCommandProvider<"SELECT ...",
ConnectionStringOrName = "name=DefaultConnection",
SingleRow = true,
AllParametersOptional = true>
let cmd = new SelectCmd(??????)"name=DefaultConnection"告诉提供程序在编译时使用app.config键。?????是您需要指定运行时连接字符串的地方。要从app.config读取连接字符串,可以使用标准的.NET方法喜欢使用ConfigurationManager
open System.Configuration
let conn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
let cmd = new SelectCmd(conn)这样的东西在将连接字符串传递给SqlCommandProvider时不能工作,因为这需要运行一些代码来读取字符串,这在运行时是可能的。这就是为什么SQL命令提供程序有一个方便的选项将name=DefaultConnection指定为特殊字符串。
https://stackoverflow.com/questions/40364229
复制相似问题