Transfer a hierarchy of entities from one sql database to another


  • If want to transfer one or more entitities and their related ones from one sql database to another one.

    What would be the easiest way to do this programatically ?

    Friday, March 22, 2013 9:13 AM


  • Code sample for code behind copy:

    1st.  Create a LinqToSQL datacontext containing the table you want to copy.  NOTE! If this table has a system generated PK then this might not work.

    2nd.  The following code will do a copy (Row by Row)

    class MainWindow
    	private string FromDBString = "Data Source=DUALCORE\\SQLExpress;Initial Catalog=MusicInfoRationialized;Integrated Security=True";
    	private string ToDBString = "Data Source=DUALCORE\\SQLEXPRESS;Initial Catalog=TestOfCopyUsingLInq;Integrated Security=True";
    	private void Button_Click(System.Object sender, System.Windows.RoutedEventArgs e)
    		FromDBDataContext fromdb = new FromDBDataContext(FromDBString);
    		object res = from ra in fromdb.RArtistsra200;
    		FromDBDataContext todb = new FromDBDataContext(ToDBString);
    		int stp = 1;

    The trick to this is to have the same datacontext but using the correct constructor which takes a datasource connection string.

    Hope this helps


    Lloyd Sheen

    Lloyd Sheen

    • Marked as answer by RolfS1 Tuesday, April 02, 2013 7:07 AM
    Friday, March 22, 2013 1:35 PM