F#:将数据加载到SQL Server

时间:2015-05-12 13:11:15

标签: sql sql-server f# type-providers

我是编程新手,F#是我的第一语言。

我想使用F#代码将数据加载到SQL Server数据库中。这是应该存储我的数据的表:

CREATE TABLE [dbo].[Scores](
    [EventName] [nvarchar](100) NOT NULL,
    [Winner] [nvarchar](50) NOT NULL,
    [Loser] [nvarchar](50) NOT NULL,
    [Score1] [nvarchar](10) NOT NULL,
    [Score2] [nvarchar](10) NOT NULL,
    [Score3] [nvarchar](10) NOT NULL
) ON [PRIMARY]

Score1Score2Score3应该包含评委的分数,有时可能无法获得。

以下是我加载数据的F#代码:

            new dbSchema.ServiceTypes.Fights(EventName = fightSummary.event,
                                            Winner = fightSummary.winner.Value,
                                            Loser = fightSummary.loser.Value,

                                            Score1 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[0]
                                                     else
                                                        "None",
                                            Score2 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[1]
                                                     else 
                                                        "None",
                                            Score3 = if judgesScoresExist then
                                                        fightSummary.judgesScores.Value.[2]
                                                     else "None")

当我尝试运行上面的F#代码时,收到以下错误消息:

  

预计此表达式具有类型       string   但这里有类型       'a *'b

显然,编译器会解释部分

"None", Score2 = ...

作为一个元组。

我尝试在线搜索解决方案,但尚未找到解决方案。

我应该做哪些更改才能将数据加载到SQL Server中?

1 个答案:

答案 0 :(得分:3)

if then else个表达式放在括号中:

let v = new Fights(EventName = fightSummary.event,
                   Winner = fightSummary.winner.Value,
                   Loser = fightSummary.loser.Value,

                   Score1 = (if judgesScoresExist then
                               fightSummary.judgesScores.Value.[0]
                             else
                               "None"),
                    ...) // etc.