Search code examples
spotfire

Fastest way to write to database from script


SQL server write script:

sqlCommand = "INSERT INTO ...

ds = DatabaseDataSource(DatabaseDataSourceSettings("System.Data.SqlClient","Server=xxx;Database=xxx;UID=xxx;PWD=xxx",sqlCommand))

#execute by creating a temp table with db source, then drop table
newDataTable = Document.Data.Tables.Add("temp",ds)
Document.Data.Tables.Remove(newDataTable)

However there is a slight delay in this method. Is it possible to do this without creating a temp table?


Solution

  • Thought I'd answer my own question from ages ago. It's possible to avoid the Spotfire libraries and use .NET System.Data.SqlClient for a much faster execution with this simple script:

    import clr, datetime
    clr.AddReference('System.Data')
    from System.Data import SqlClient
    from System import Threading, DateTime
    
    sql = "INSERT INTO ...."
    
    conn = SqlClient.SqlConnection("Server=;Database=;UID=;PWD=")
    conn.Open()
    
    cmd = SqlClient.SqlCommand(sql, conn)
    exe = cmd.ExecuteReader()
    
    exe.Close()
    conn.Close()