. Net (1): settings. Settings

Source: Internet
Author: User

If you select "save" and "database connection string" when designing the. dbml file in vs2008, the information will be stored in the propreties/settings. settings. CS file.

What should I do if I want to change the connection string during release? In fact, all the secrets are in the app. config file.

When setting. settings is generated in vs2008, the information in this file is already stored in APP. config. You only need to modify the object information in config to dynamically change the database connection string and other content.

If you want to reference the dbml file in a web projectProgramCopy the information of the app. config file generated by the set to the Web. config file.

 

Note: The *. dbml file also stores the connection string information, which is supported during vs2008 design.

Follow these steps to change the connection string of the dbml file: In the designer, select dbml File> Properties window> connection and modify it.

 

SimpleCodeAs shown in the following figure, you can see the patterns they constitute.

 

Dbml XML file. Pay attention to the connection node

<? XML version = "1.0" encoding = "UTF-8" ?>
< Database Name = "Database1" Class = "Dataclasses2datacontext" Xmlns = "Http://schemas.microsoft.com/linqtosql/dbml/2007" >
< Connection Mode = "Appsettings" Connectionstring = "Data Source = server2003bak; initial catalog = mytest; user id = sa" Settingsobjectname = "Windowsformsapplication1.properties. Settings" Settingspropertyname = "Connstring" Provider = "System. Data. sqlclient"   />
< Table Name = "DBO. Table2" Member = "Table2" >

 

 

Settings. settings. CS File Content

Namespace Windowsformsapplication1.properties {


[ Global : System. runtime. compilerservices. compilergeneratedattribute ()]
[ Global : System. codedom. compiler. generatedcodeattribute ( " Microsoft. visualstudio. Editors. settingsdesigner. settingssinglefilegenerator " , " 9.0.0.0 " )]
Internal   Sealed   Partial   Class Settings: Global : System. configuration. applicationsettingsbase {

Private   Static Settings defaultinstance = (Settings )( Global : System. configuration. applicationsettingsbase. synchronized ( New Settings ())));

Public   Static Settings default {
Get {
Return Defaultinstance;
}
}

[ Global : System. configuration. applicationscopedsettingattribute ()]
[ Global : System. Diagnostics. debuggernonusercodeattribute ()]
[ Global : System. configuration. specialsettingattribute ( Global : System. configuration. specialsetting. connectionstring)]
[ Global : System. configuration. defaultsettingvalueattribute ( " Data Source = server2003bak; initial catalog = mytest; user id = sa; Password = 123 " )]
Public   String Connstring {
Get {
Return (( String )( This [ " Connstring " ]);
}
}
}
}

 

Automatically Generated app. config file

 

<? XML version = " 1.0 " Encoding = " UTF-8 " ?>
< Configuration >
< Configsections >
</ Configsections >
< Connectionstrings >
< Add name = " Windowsformsapplication1.properties. settings. connstring "
Connectionstring = " Data Source = server2003bak; initial catalog = mytest; user id = sa; Password = 123 "
Providername = " System. Data. sqlclient "   />
</ Connectionstrings >
</ Configuration >

 

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.