0

I want to use connection string defined into Web.Config file into my Class code which is using ConnectionInfo keyword.

I have defined my connection string in Web.Config file as::

 <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>

And in my Class code I want to use it into ConnectionInfo Currently I have to define it in conroller as::

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

But I want to use it from Connection string Defined into Web.Config file.

0

1 Answer 1

1

will this help you:

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;

And then in your ConnectionInfo:

ConnectionInfo ConnInfo = new ConnectionInfo { ServerName = "dbDataSource", UserID = "userId", Password = "pass", DatabaseName = "dbName" };
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.