Solution to connection character escape

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.