josecortesp
josecortesp

Reputation: 1576

How do I Programmatically change Connection String un LINQ C# Winforms

This question may be redundant, but i could not totally understand how to do this. I need to be able to, when the users Run my Winforms app, can search for the instance of SQL if the previous one are not available. I already have the check for the DB existence, and also made a dialog wich search for all the available instance, and bulding the connection string isn't a problem. The point here is that, I need to be able to everytime the users open the app it loads the CN from a external file, and if the external file doesn't exist or the instance isn't available, i can use the app in another instance (asuming, offcourse, that the required DB is in that Instance). The point is that, i don't know how to Programmatically change Connection String, using LINQ in winforms. Thanks in advance

Upvotes: 2

Views: 1874

Answers (3)

David
David

Reputation: 8650

A DataContext is created per 'unit of work'.

As ichiban says pass the required connection string into the constructor when creating the DC

Upvotes: 0

var someConnectionString = "This is my connection String";

using (var db = new SomeConcreteDataContext(someConnectionString)){
    //...Do whatever...
}

Upvotes: 1

ichiban
ichiban

Reputation: 6200

You should be to pass the connection string to the DataContext constructor.

var db = new MyDataContext(myconnectionstring);

Upvotes: 5

Related Questions