ConfigurationManager class publicstatic class ConfigurationManager namespace: System.Configuration Assembly: System.Configuration (in System.Configuration.dll) configurationmanager.appsettings The Configurationmanager.connectionstrings property gets the connectionstringssection data for the current application's default configuration. ConfigurationSettings class Namespace: system.configuration assembly: System (in System.dll) properties: AppSettings obsolete. The ConfigurationSettings class provides backward compatibility only. For new applications, you should use the ConfigurationManager class or the WebConfigurationManager class instead. To use these two classes, you must add a reference to the System.Configuration namespace in your project or application. The SqlConnection.ConnectionString property gets or sets the string that is used to open the SQL Server database. Namespace: System.Data.SqlClient assembly: System.Data (in System.Data.dll)
usingSystem.Data.SqlClient;usingSystem.Configuration;namespaceWindowsFormsApplication1 { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidForm1_Load (Objectsender, EventArgs e) {SqlConnection conn; stringSQLSTR = configurationmanager.connectionstrings["sqlconnstring"]. ConnectionString; Conn=NewSqlConnection (SQLSTR);//instantiating a SqlConnection database connection objectConn. Open ();//Open a database connection if(Conn. state = = ConnectionState.Open)//determine if the connection is open{Label1. Text="SQL Server database connection open! "; } } }}
when we write a C # application, we place an app. config in the project file, and when the program is packaged, the configuration file is automatically compiled into a. exe.config file with the same name as the assembly. The effect is that after the application is installed, you simply need to locate the file in the installation directory and change the string contents to alter the running parameters without modifying the source code. For example, you can use a configuration file to save a database connection string, display a changed text message in your application, and so on. Because of their knowledge of how to use the method, most people choose to rebuild the configuration file themselves and manage it themselves, in fact, the file can be manipulated by the user. The app. Config file is an XML document format that allows you to add any number of strings to the file that the application can read at run time.
To add a portion of the following red line painting:
Click Project, add New item, select the item shown
The contents of the file are as follows:
Content of the app. Config file:
<?xml version="1.0"?><configuration> <connectionStrings> <add name="sqlconnstring" connectionstring="server=localhost;d Atabase=my; Uid=sa; pwd=" providername="System.Data.SqlClient "/> </ Connectionstrings></configuration>
Running results such as:
Next we go to the directory where the solution is located and then use Notepad to open ConSQL.exe.config this file to make the following changes and then double-click the ConSQL.exe the file is now in this case:
Connect the database through the configuration File App. Config