Given the security of the code and the ease with which it has been modified, we often do not place the database connection string directly in the code, but instead place the string in an app. config configuration file, assign it to a variable, and then reference the variable in the code.
Here's how to do this: ① right-click the project name and create a new application configuration file that is renamed App. Config. ② Add the following code in app. Config
1 <configuration>2 <connectionStrings>3 <add name=" Myconnstr " connectionstring="Data source=pc201507182002\sqlexpress;initial catalog=123456;integrated Security=true"/>4 </connectionStrings>5 </configuration >
③ add a name for theSystem.ConfigurationReference, and then change the original code to create the connection string to the following.
string connstr = configurationmanager.connectionstrings["connstr"]. ConnectionString; using New SqlConnection (CONNSTR))
Note: In the above code, the green ConnStr is the name value of add in the config File App. Config.
(4) C # ADO to put the database connection string into the configuration file