简体   繁体   English

F# SQL 将 obj[][] 写入 SQL 表

[英]F# SQL write an obj[][] to an SQL table

So I have an obj[][] that I've created using F# and now I want to write the values in that collection to an SQL table.所以我有一个使用 F# 创建的 obj[][] ,现在我想将该集合中的值写入 SQL 表。 The issue is this needs to be able to upload to any table that I specify.问题是这需要能够上传到我指定的任何表。 Meaning the name of the table is stored as a variable and the length of the collection will also change.这意味着表的名称存储为变量,并且集合的长度也会发生变化。

let tableName = "table2"

my object array is called dataTable so:我的对象数组被称为 dataTable 所以:

dataTable[0][0] = "Jackson"
         [0][1] = "Sentzke"
         [0][2] = "1991-04-19T00:00:00"
         [0][3] = "Jackson Sentzke"
         [1][0] = "Peter"
         [1][1] = "Sentzke"
         [1][2] = "1962-02-11T00:00:00"
         [1][3] = "Peter Sentzke"

This isn't how I actually get the values from dataTable, this is just an example.这不是我实际从 dataTable 获取值的方式,这只是一个示例。

Anyone know how I'd be able to insert the data from dataTable into "table2"?任何人都知道我如何能够将 dataTable 中的数据插入到“table2”中?

At the moment I have:目前我有:

let cn = new SqlConnection(connectionstring)
cn.Open()
let sQL = "INSERT INTO [" + tableName + "] VALUES(@param1)"
let db = new SqlCommand(sQL, cn)
db.Parameters.AddWithValue("@param1", tableData)
db.ExecuteNonQuery()
    let cn = new SqlConnection(connectionstring)
    cn.Open()

    for i=0 to tableData.Length-1 do
        let mutable columnsTemp = ""
        let mutable valuesTemp = ""
        for j = 0 to tableData.Item(i).Length-2 do
            columnsTemp <- columnsTemp + columnNames.GetValue(j).ToString() + ","
            valuesTemp <- valuesTemp + "'" + tableData.Item(i).GetValue(j).ToString() + "',"


        let columns = columnsTemp.Remove(columnsTemp.Length-1,1)
        let values = valuesTemp.Remove(valuesTemp.Length-1,1)
        let sQL = "INSERT INTO [" + tableName + "] (" + columns + ") VALUES (" + values + ")"
        let db = new SqlCommand(sQL, cn)
        db.ExecuteNonQuery()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM