如何使用SQLProvider在F#中连接到MS SQL Server?

4

我试图使用SQLProvider连接F#和MS SQL Server,但似乎无法在建议的环境下实现。

请查看我的模块如下:

namespace MyApp

open FSharp.Data.Sql

module Database =

    [<Literal>]
    let dbVendor = Common.DatabaseProviderTypes.MSSQLSERVER


    [<Literal>]
    let connString =
        "Data Source=localhost;Database=MyDatabase;User Id=user;Password='my-password-here'"

    [<Literal>]
    let indivAmount = 1000

    [<Literal>]
    let useOptTypes = true

    type sql = SqlDataProvider<dbVendor, connString, IndividualsAmount=indivAmount, UseOptionTypes=useOptTypes>

我的主要问题在于加入这段代码后,我在Intellisense中看到了以下信息/警告:

类型提供程序“FSharp.Data.Sql.SqlTypeProvider”报告了一个错误:System.Data.SqlClient在此平台上不受支持。F#编译器(3033)

并且在尝试编译时出现了构建错误:

Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]

Build FAILED.

C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
C:\Development\F#\MyApp\DataTypes.fs(21,16): error FS3033: The type provider 'FSharp.Data.Sql.SqlTypeProvider' reported an error: System.Data.SqlClient is not supported on this platform. [C:\Development\F#\MyApp\MyApp.fsproj]
    0 Warning(s)
    2 Error(s)

Time Elapsed 00:00:06.21

我是否做错了什么,或者说默认设置无法与最新的.NET和F#一起使用,我需要提交一个git问题吗?欢迎任何见解和批评,谢谢!

发布时所使用的软件:

  • .NET 6
  • VS Code (1.62.2)
  • Ionide (v5.9.0)
  • SQLProvider 版本1.2.10
  • System.Data.SqlClient 版本4.8.3

2
不是答案,但你尝试过按照链接页面中所述使用“Microsoft.Data.SqlClient.dll”而不是内置的“System.Data.SqlClient.dll”吗? - David Browne - Microsoft
不完全是,我尝试将其包含为参考,但那是我所做的最多的。@DavidBrowne-Microsoft,您是否知道如何按照他们想要的方式链接dll?文档在这方面相当模糊,即“只需执行X”,而没有真正展示如何操作。更详细解释的链接会很有帮助。 - CoderLee
从这里下载nuget包:https://www.nuget.org/packages/Microsoft.Data.SqlClient/,然后将其重命名为`.zip`扩展名并提取.dll文件。 - David Browne - Microsoft
@DavidBrowne-Microsoft 我只需要将dll放在F#项目的任何位置并在编译后使用它被放置的位置(或项目内的位置)作为 ResolutionPath吗? - CoderLee
1
为什么要通过下载文件来解决依赖关系,而不是在项目文件夹中直接使用 dotnet add package Microsoft.Data.SqlClient 呢? - masoodahm
显示剩余2条评论
2个回答

3
为了解决这个问题,我在Visual Studio (v17 2022)中打开了该项目,并执行以下操作:
  • 删除相关依赖项
  • 重新添加SQLProvider和System.Data.SqlClient (v4.8.3)
  • 清空bin和obj文件夹
完成上述步骤后,我能够运行应用程序,连接到数据库,并执行简单的select查询,并返回预期数据。
不知道为什么,但即使构建正常,Ionide也总是显示可怕的红色波浪线。

1

从命令行运行:

dotnet add package microsoft.data.sqlclient

然后将 Common.DatabaseProviderTypes.MSSQLSERVER 更改为 Common.DatabaseProviderTypes.MSSQLSERVER_DYNAMIC


1
这对我没有用,它无法加载所需的资源,并且在SqlDataProvider上编译时会出现FS3033错误。有什么想法或如何解决? - CoderLee
我认为问题源于VS Code使用dotnet而不是msbuild。显然,他们集成sqlclient的方式与dotnet build/run等不兼容。我已经在这个问题上花了一个月的时间,但还是无法解决它。 - zelarian

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接