有两种方式在web.config/app.config中设置ConnectionString.
一种是在ConnectionString中设置
例如
XML code
<connectionStrings>
<add name=
"ConnectionString"
connectionString=
"Server=xxx; Port=3306; Database=xxx; uid=xxx; pwd=xxx;"
/>
</connectionStrings>
在代码中读取使用
C# code
System.Configuration.ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString
另一种是在AppSettings中设置
XML code
<appSettings><add key=
"ConnStr"
value=
"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\MyData\App_Data\Database.mdf;Integrated Security=True;User Instance=True"
/></appSettings>
在代码中读取使用
C# code
string
connStr = System.Configuration.ConfigurationManager.AppSettings[
"ConnStr"
].ToString();