Code
Linqtosql will scatter the connection strings in multiple locations, and it cannot be changed after compilation.
This makes it inconvenient. For example, After configuringProgramOn the other machine, you have to find all the scattered connection strings, and then modify them one by one to run.
You may want to read the connection string from the configuration file when using linqtosql.
The following tips Let us achieve this goal.
1: Add a linqtosql class to the project. Assume the class name is dataclasses1.
So. NET will automatically generate a bunch of things:
Double-click dataclasses1.dbml to open the design page. Drag several tables from the serverexpoloer window to the design page and save the settings.
2: In the dataclasses1.designer. CS file, find the no-argument constructor of dataclasses1datacontext (it has many constructor methods and only the one without parameters ).
Cut this method out.
3: Add a new class in the project to ensure that the class name is the same as that of the generated datacontext class, which is called dataclasses1datacontext. Add the partial modifier to the front of the class definition.
4: Copy the cut method to the newly added class. Modify the parameters behind the base to the connection string in your configuration file.
The final effect is as follows.
5: If you want to delete the settings file, delete it because it will be automatically generated when you drag the database class to dbml.
6: Enter the dbml design interface, right-click in the blank space, and click "properties ". In the Property bar, set "connection" to "NONE ".
After the above six steps, your LINQ will only connect to the database through the connection string in the configuration file, without worrying about where your representation is dragged.
Every time you drag a new class into dbml, repeat Step 6.
Everything is okay. From now on, linqtosql is moving freely ~~
http://vstohome.com/archives/4157