WinForm的App.config写法
1.App.config文件写法
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="conn" value="Data Source=.\sqlexpress;Initial Catalog=School;Integrated Security=True"/> </appSettings> </configuration>
调用如下:
public static string connStr = ConfigurationManager.AppSettings["conn"].ToString();
2.App.config文件写法
<?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="TrackingDatabase" connectionString="Data Source=127.0.0.1;Initial Catalog=WorkflowTracking;User ID=sa;Password=123" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>
调用如下:
String conn = ConfigurationManager.ConnectionStrings["TrackingDatabase"].ConnectionString.ToString();
在c#源代码中使用 ConfigurationManager 这个类,引用using System.Configuration这个包,解决方案也需要引用Configuration