F# LINQ add new rows to SQL Server

2019-06-26 03:21发布

I am learning LINQ with F# 3.0. I want to know how to re-write my old code to use new LINQ features in F# 3.0 For example, I have created a simple data table in SQL Server 2008 R2, the database name is myDatabase.

-- Create the Table1 table.
CREATE TABLE [dbo].[Table1] (
    [Id]        INT        NOT NULL,
    [TestData1] INT        NOT NULL,
    [TestData2] FLOAT (53) NOT NULL,
    [Name]      NTEXT      NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);

For F# 2.0, I can use Dataset to add new rows, like this:

#light
open System
open System.Collections.Generic 
open System.Data
open System.Data.SqlClient

let sqlConn = "server=(local); Integrated Security=True; Database=MyDatabase"
let connDB = new SqlConnection(sqlConn)
let sql = "SELECT * FROM Table1"
let da = new SqlDataAdapter(sql, connDB)
let ds  = new DataSet()
da.Fill(ds) |> ignore
let commandBuilder: SqlCommandBuilder = new SqlCommandBuilder(da)

for i = 0 to 1 do
    let newDR: DataRow = ds.Tables.Item(0).NewRow()
    newDR.Item(0) <- (i + 1)
    newDR.Item(1) <- (i * 10)
    newDR.Item(2) <- ( decimal i ) * 5.0M
    newDR.Item(3) <- "Testing" + (i + 1).ToString()
    ds.Tables.Item(0).Rows.Add(newDR)
da.Update(ds) |> ignore

Now with F# 3.0, how I can re-write the add new rows code better? I think I can wrtie some code like:

#light
open System
open System.Data.Linq
open Microsoft.FSharp.Data.TypeProviders
open Microsoft.FSharp.Linq

[<Generate>]
type dbSchema = SqlDataConnection<"Data Source=.;Initial Catalog=MyDatabase;Integrated Security=True">
let db = dbSchema.GetDataContext()

try
   db.DataContext.ExecuteCommand("INSERT INTO Table1 (Id, TestData1, TestData2, Name) VALUES (1, 10, 0.0, 'Testing1')") |> ignore
with
   | exn -> printfn "Exception:\n%s" exn.Message

try
   db.DataContext.ExecuteCommand("INSERT INTO Table1 (Id, TestData1, TestData2, Name) VALUES (2, 20, 5.0, 'Testing2')") |> ignore
with
   | exn -> printfn "Exception:\n%s" exn.Message

But I don't think the new way is better, in fact, I think it is even worse. In F# 2.0, I can use code to generate the values for data table, but if I have to write the static SQL statement, like "INSERT INTO Table1 VALUES(" with prefined values, then I think I would prefer to insert data records by hand from SQL Server Management Studio, as I can see the results immediately. Anyone has a better idea on this? Thanks, John

1条回答
贪生不怕死
2楼-- · 2019-06-26 03:44

You can use InsertOnSubmit and InsertAllOnSubmit for nice, statically typed record insertion. Something like:

let newRecords =
    [for i in [0 .. 1] ->
        new Table1(Id=(i + 1), TestData1=(i * 10), TestData2=( decimal i ) * 5.0M, Name="Testing" + (i + 1).ToString())]

db.Table1.InsertAllOnSubmit(newRecords)
db.SubmitChanges()

Also see http://msdn.microsoft.com/en-us/library/hh361033(v=vs.110).aspx and search within the page for "InsertOnSubmit" (in fact, the example there is made for your Table1 demo table).

查看更多
登录 后发表回答