由于现在不论是连接数据库还是连接数据库实体,都需要大量使用连接字符串来获取数据库或者实体的位置,所有我们应该把它尽可能多的放在app.config这个系统配置文件中,以便方便我们的读取
首先,来了解一下app.config这个文件的结构
在<connectionStrings>
这个节中,我们可以看到name为“bindingDemo.Properties.Settings.AdventureWorksConnectionString”的字符串,下面的"connectionString“便是我们需要的连接字符串内容。
下面我们在winform程序中获取此字符串变量:
到此,连接字符串变引用成功了。
这里需要注意一点,VS2010中,连接字符串的引用方法有了稍许的变化,需要调用Properties.Settings.Default.(App.config中<connectionStrings>节点下面Add的name值)