1

I need to clone a table of a database which exists in a different servers. e.g.On server A there is a database called EmployeeDataBase which has a table t1 which I need to copy to the database called EmployeeDataBase which exists on Server B. How to do it using linq query.

My application uses linq to entity.

Thanks

user650922
  • 2,071
  • 3
  • 17
  • 23

2 Answers2

2

If the tables have the same definition you can use the same mappings, just you need to create different contexts using the appropriate connection strings.

var ctxSource = new  EmployeeDataBaseContext("[source connection]");
var ctxDestination = new  EmployeeDataBaseContext("[destination connection]");

ctxDestination.t1.InsertAllOnSubmit(ctxSource.t1.ToList());
ctxDestination.SubmitChanges();

Check here the constructors you need.

Adrian Iftode
  • 15,465
  • 4
  • 48
  • 73
  • Let me elaborate:I have a textbox for server name,username and password required for a connection.When a user after filling up these textboxes clicks on a dropdown I check the connection and load all the databases in a database dropdown.Now User selects a source database from the givend dropdown.when a user selects on a particular database all the distinct rows of that database should be populate in other dropdown. – user650922 Jan 23 '12 at 05:53
  • so you don't know in advance the database schema – Adrian Iftode Jan 23 '12 at 05:58
  • DataContext lnqDC = new System.Data.Linq.DataContext("Data Source=" + serverName + ";Initial Catalog=master;User Id=" + serverUserName + ";Password=" + serverPassWord + ";"); var DatabaseNames = lnqDC.ExecuteQuery("select[name] from sys.databases").AsEnumerable();I did this to list all the databases of a server. – user650922 Jan 23 '12 at 06:45
  • this is totally another problem – Adrian Iftode Jan 23 '12 at 06:59
  • Got it.I did exactly what you told on the first answer. – user650922 Jan 23 '12 at 09:00
  • can you help me with this linq query:http://stackoverflow.com/questions/38120664/how-to-group-by-on-2-child-entities-and-get-total-of-both-this-child-entities – I Love Stackoverflow Jun 30 '16 at 12:50
2

Entity Framework is not the right technology for this kind of problem. You will be better off using raw ADO.Net. Maybe SqlBulkCopy can be of use: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlbulkcopy.aspx

jeroenh
  • 26,362
  • 10
  • 73
  • 104