Three Methods for C in VS to Read app. config Database Configuration String

  • 2021-08-17 00:48:37
  • OfStack

Three methods of selecting database configuration string in VS2008 or VS2005
When VS 2008 builds the Form program, if the data source is added, the connection string will be automatically written in the configuration file app. config. This string will prompt you to select or create a new string when you use DataSet, SqlDataAparter, SqlConnection and other controls. 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 device (available locally LocalHost, If it is EXPRESS The development version must be .\SQLEXPRESS);Initial Catalog= Database name ;User ID= User name ;Password= Your own password  />
  <add key="TemplatePATH" value="Template" />
</appSettings>
<connectionStrings>
    <add name="SxzzManager.Properties.Settings.sxzzConnectionString"
      connectionString="Data Source= Your database machine ;Initial Catalog= Database name ;User ID=sa;Password= Your own password "
      providerName="System.Data.SqlClient" />
    <add name="TestConnectionString" connectionString="Data Source= Your database machine ;Initial Catalog= Database name ;User ID=sa;Password= Your own password "
      providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Description:
Among them
"1"

< appSettings >
< add key = "connectionstring" value = "Data Source = your database machine (LocalHost is available locally, if EXPRESS development version must be.\ SQLEXPRESS); Initial Catalog = database name; User ID = user name; Password = your own password/ >
< add key="TemplatePATH" value="Template" / >
< /appSettings >

Added by hand.

[2]
< add name="SxzzManager.Properties.Settings.sxzzConnectionString"
connectionString= "Data Source= your database machine; Initial Catalog = database name; User ID = sa; Password = your own password "
providerName="System.Data.SqlClient" / >
Content that is automatically generated when a data source is added in VS 2008.

[3]
< add name= "TestConnectionString" connectionString= "Data Source= Your database machine; Initial Catalog = database name; User ID = sa; Password = your own password "
providerName="System.Data.SqlClient" / >
For manual addition.

To get 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 VS 2008 menu =" Project = "Attributes =" Settings = "name pop-up dialog box. Is actually the string after the last point in "SxzzManager. Properties. Settings. sxzzConnectionString".

To get the connection string shown in "3", you can use one string in "2"

string constr = ConfigurationManager.ConnectionStrings["TestConnectionString"].ToString();
string constr = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;

Add: If VS does not recognize the ConfigurationManager class, you need to add the "System. Configuration" reference to the reference


Related articles: