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 >