将数据库的连接字符串放在配置文件中:
<connectionStrings>
<add name="connStr" connectionString="Data Source=.; Initial Catalog=MyTest1;User ID=sa;Password=sasa"/>
</connectionStrings>
然后,要在项目中添加对System.Configuration的引用:
private string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;