same database on multiple servers

2 posts, 0 answers
  1. Ken
    Ken avatar
    18 posts
    Member since:
    Nov 2010

    Posted 30 Nov 2010 Link to this post

    We have the same database on multiple servers. How can I programmatically via a web app change connection on the fly to a different server. All databases have the same name and schema.


    Thanks
  2. Alexander
    Admin
    Alexander avatar
    727 posts

    Posted 01 Dec 2010 Link to this post

    Hello Ken,

    You can use the constructor of the OpenAccessContext class which takes a connection parameter and pass a connection string or connection string name (if the connection string is itself present in the .config file). To switch to another server it is enough just to create a new context instance with the appropriate connection string. Here is an example:

    string connectionOne = @"data source=.\sqlexpress;initial catalog=Northwind;integrated security=True";
    string connectionTwo = @"data source=.\MSSQL;initial catalog=Northwind;integrated security=True";
     
    //Work with database one
    NorthwindEntityDiagrams context = new NorthwindEntityDiagrams(connectionOne);
    Category category = new Category() { CategoryName = "CategoryOne" };
    context.Add(category);
    context.SaveChanges();
     
    //Work with database two
    context = new NorthwindEntityDiagrams(connectionTwo);
    category = new Category() { CategoryName = "CategoryTwo" };
    context.Add(category);
    context.SaveChanges();

    Hope that helps.

    Best wishes,
    Alexander
    the Telerik team
    Accelerate your learning with industry's first Telerik OpenAccess ORM SDK. Download today.
  3. DevCraft banner
Back to Top