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

时间:2016-10-12 10:56:09

标签: sql arrays f# insert ado.net

所以我有一个使用F#创建的obj [] []现在我想将该集合中的值写入SQL表。问题是这需要能够上传到我指定的任何表。这意味着表的名称存储为变量,集合的长度也将更改。

let tableName = "table2"

我的对象数组名为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"

这不是我实际从dataTable获取值的方式,这只是一个例子。

任何人都知道如何将dataTable中的数据插入“table2”?

目前我有:

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()

1 个答案:

答案 0 :(得分:1)

    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()