使用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=&quot;data source=servername;initial catalog=dbName;user id=username;password=password;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

在我的类代码,我想使用它到ConnectionInfo目前我必须在控制器中将其定义为::

ConnectionInfo ConnInfo = new ConnectionInfo { ServerName = "servername", UserID = "UserID", Password = "Password", DatabaseName = "DatabaseName" };

但我想使用它从连接字符串定义到Web。配置文件。

使用Web连接字符串.在ConnectionInfo中配置

这对你有帮助吗?

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" };