Three ways to configure a database configuration string in VS2008 or VS2005
VS2008 When you create a form program, if you add a data source that automatically writes the connection string in the config file App. Config, this string will be used when you use Dataset,sqldataaparter, SqlConnection, such as the shadow prompts you to choose, or a new string. If you want to get this string in code, there are three ways:
App. Config content:
<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key= "ConnectionString" value= "Data source= your database (localhost available, if the Express development version must be. \sqlexpress); Initial catalog= database name; User id= username; Password= your own password/>
<add key= "TemplatePath" value= "Template"/>
</appSettings>
<connectionStrings>
<add name= "SxzzManager.Properties.Settings.sxzzConnectionString"
connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
<add name= "testconnectionstring" connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
</connectionStrings>
</configuration>
Description
which
"1"
< appsettings>
<add key= "ConnectionString" value= "Data source= your database (localhost available, if the Express development version must be. \sqlexpress); Initial catalog= database name; User id= username; Password= your own password/>
<add key= "TemplatePath" value= "Template"/>
</appSettings>
To be added by hand.
"2"
<add name= "SxzzManager.Properties.Settings.sxzzConnectionString"
connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
The content that is automatically generated when you add a data source in VS2008.
"3"
<add name= "testconnectionstring" connectionstring= "Data source= your database device; Initial catalog= database name; User Id=sa; Password= Your own password "
Providername= "System.Data.SqlClient"/>
Added for manual.
To obtain the connection string shown in "1", use the following statement:
String constring = system.configuration.configurationsettings.appsettings["ConnectionString"];
String constring = system.configuration.configurationmanager.appsettings["ConnectionString"];
There are two ways to get the connection string shown in "2"
<1>: string constr = system.configuration.configurationmanager.connectionstrings["SxzzManager.Properties.Settings.sxzzConnectionString"]. ToString ();
<2>: string constr = sxzzmanager.properties.settings.default["sxzzconnectionstring"]. ToString ();//"where" sxzzconnectionstring "can be displayed in the dialog box that pops up in the VS2008 menu =" Project = "Property =" set = "name. is actually "SxzzManager.Properties.Settings.sxzzConnectionStringString after the last point in the.
To get the connection string shown in "3", a string in "2" can be used
String constr = configurationmanager.connectionstrings["testconnectionstring"]. ToString ();
String constr = configurationmanager.connectionstrings["testconnectionstring"]. ConnectionString;
If it appears: Configuration system failed to initialize error, turn configurationstring into appseting