1.在<connectionStrings> 标签里添加连接
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;Database=DatabaseName;UserID=sa;Password=abc123"
providerName="System.Data.SqlClient" />
</connectionStrings>
或者
<connectionStrings>
<add name="ConnectionName" connectionString="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
在程序代码中获取<connectionStrings> 标签里的连接字符串:
引用命名空间:
- Using System.Configuration ;
- string connStr = ConfigurationManager.ConnectionStrings["ConnectionName"].ToString();
2.在<appSettings>标签里添加连接
<appSettings>
<add key="ConnectionName" value="Server=.\SQLEXPRESS;Database=DatabaseName;User ID=sa;Password=abc123" />
</appSettings>
或者
<appSettings>
<add key="ConnectionName" value="Server=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\data.mdf;Integrated Security=True" />
</appSettings>
在程序代码中获取<appSettings> 标签里的连接字符串:
引用命名空间:
- Using System.Configuration ;
- string connStr = ConfigurationManager.AppSettings["ConnectionName"].ToString();