SQL Server Developer Center > SQL Server Forums > SQL Server SMO/DMO > Failed to transfer primary key and foreign key
Ask a questionAsk a question
 

AnswerFailed to transfer primary key and foreign key

  • Thursday, October 15, 2009 5:30 AMGurmit Teotia Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hello,

    We're trying to transfer database from SQL 2005 to SQL 2008 with following code-


    Server

    srcSrv = new Server("einpxp102\\sqlexpress");

     

    Server dstSrv = new Server("einpxp102");

     

     

    // Get source database from the source server.

     

    Database srcDb = srcSrv.Databases[srcDatabaseName];

     

    // Create backup databse on the destination server

     

    Database dstDb = new Database(dstSrv, dstDatabaseName);

    dstDb.Create();

    // This creates the new database on the 'destination' server.

     

    // Create transfer.

     

    Transfer t = new Transfer(srcDb);

    t.CopyAllObjects =

    true;

    t.CopySchema =

    true;

    t.CopyData =

    true;

     

    // Set the destination table for the transfer object.

    t.DestinationDatabase = dstDb.Name;

     

    // Now set up the destination server

    t.DestinationServer = dstSrv.Name;

     

    // and to the transfer, this should do it.

    t.TransferData();

    But it is not transferring primary keys and foreign keys. I'd appreciate any help on it.

    Regards,
    Gurmit

Answers

  • Friday, October 16, 2009 9:05 AMSateesh Pragallapati-MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    For copying the Keys (primary keys or foreign keys), you have to set transfer option.

    //this option copies all the keys. By default its value is false.
    t.Options.DriAllKeys = true;


    Thanks,
    Sateesh Pragallapati.
    Mark Post as helpful if it provides any help.Otherwise,leave it as it is.

All Replies

  • Friday, October 16, 2009 9:05 AMSateesh Pragallapati-MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    For copying the Keys (primary keys or foreign keys), you have to set transfer option.

    //this option copies all the keys. By default its value is false.
    t.Options.DriAllKeys = true;


    Thanks,
    Sateesh Pragallapati.
    Mark Post as helpful if it provides any help.Otherwise,leave it as it is.