使用Web连接字符串.在ConnectionInfo中配置
本文关键字:ConnectionInfo 配置 字符串 Web 连接 使用 | 更新日期: 2023-09-27 18:04:15
我想使用定义为Web的连接字符串。配置文件到我的类代码,使用ConnectionInfo
关键字。
我在Web中定义了我的连接字符串。配置文件为::
<connectionStrings>
<add name="Connection_Report" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string="data source=servername;initial catalog=dbName;user id=username;password=password;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
在我的类代码,我想使用它到ConnectionInfo
目前我必须在控制器中将其定义为::
ConnectionInfo ConnInfo = new ConnectionInfo { ServerName = "servername", UserID = "UserID", Password = "Password", DatabaseName = "DatabaseName" };
但我想使用它从连接字符串定义到Web。配置文件。
这对你有帮助吗?
var connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["yourConnectionString"].ConnectionString;
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);
string dbName = builder.InitialCatalog;
string dbDataSource = builder.DataSource;
string userId = builder.UserID;
string pass = builder.Password;
然后在你的ConnectionInfo
中:
ConnectionInfo ConnInfo = new ConnectionInfo { ServerName = "dbDataSource", UserID = "userId", Password = "pass", DatabaseName = "dbName" };