以下是我的app.config文件的片段。
<MyProject>
<ConnectionStrings>
...
<Operational providerType="SqlServer" provider="" dataSource="<ServerName>" initialCatalog="<DBName>" security="" userName="<SomeUser>" password="<Password>" />
...
</ConnectionStrings>
</MyProject>
在我的C#代码中,我在某个变量中提供了此值,例如......
string OperationalConnectionConfiurationPath = "MyProject/ConnectionStrings/Operational";
如何从中获取我的SqlConnection对象?
答案 0 :(得分:0)
connectionStrings
是configuration
部分的孩子。摆脱那些<MyProject>
的东西。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="Operational" connectionString="Data Source=SERVER_NAME;Initial Catalog=DB_NAME;User Id=USER_ID;Password=PASSWORD;" providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
您可以通过以下方式获取连接字符串:
using System.Configuration;
var connectionString = ConfigurationManager.ConnectionStrings["Operational"].ConnectionString;