简体   繁体   中英

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. 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[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.

Anyone know how I'd be able to insert the data from dataTable into "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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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