locked
How to copy a table from another database? RRS feed

  • Question

  • User2097951763 posted

    table 1in a.sdf

    how can I copy table1 to b.sdf

    Saturday, August 24, 2013 3:17 AM

Answers

  • User-821857111 posted
    var a = Database.Open("a");
    var data  = a.Query("SELECT * FROM Table1");
    var b = DatabaseOpen("b");
    //if the table already exists
    foreach(var row in data){
        b.Execute("INSERT INTO Table1 (col1,col2,col3,...) VALUES (@0, @1, @2...)", row.col1, row.col2,row.col3,...); 
    } 

    If the table doesn't exist, you need to create it. You can use CREATE TABLE for that:

    var ddl = "CREATE TABLE Table1 (Col1 IDENTITY NOT NULL PRIMARY KEY, Col2 NVARCHAR(50), Col3 DATETIME etc)";
    b.Execute(ddl); 



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, August 24, 2013 3:32 AM

All replies