0

It is possible to use app keys for all the connection string inputs and read those on connection string like bellow

<add name="DefaultConnection" connectionString="Data Source=$(Server);Initial Catalog=$(Catalog);User ID=$(User);Password=$(Password)" providerName="System.Data.SqlClient" />



<add key="$(Server)" value="xxxx" />
<add key="$(Catalog)" value="xxxx" />
<add key="$(User)" value="xxxx" />
<add key="$(Password)" value="xxxx" />
3
  • Why do you want to do that ? What are you trying to achieve ? Commented Nov 27, 2015 at 4:50
  • i need to read those values as app keys as well Commented Nov 27, 2015 at 4:52
  • What you are trying to achieve seems unnecessary, if you are not going to use the $(server) and other settings individually elsewhere in the application as well. Commented Nov 27, 2015 at 5:25

2 Answers 2

3

As @Ertürk Öztürk already say - it's not possible.

If you searching for more or less clean way to do it i suggest you to use SqlConnectionStringBuilder or DbConnectionStringBuilder if you using not MSSQL data base.

In your code it will be like this with SqlConnectionStringBuilder:

//create connection string builder
System.Data.SqlClient.SqlConnectionStringBuilder connectionStringBuilder = new System.Data.SqlClient.SqlConnectionStringBuilder();

//set all properties from your WebConfig
connectionStringBuilder.DataSource = ConfigurationManager.AppSettings["Server"];
connectionStringBuilder.InitialCatalog = ConfigurationManager.AppSettings["Catalog"];
connectionStringBuilder.UserID = ConfigurationManager.AppSettings["User"];
connectionStringBuilder.Password = ConfigurationManager.AppSettings["Password"];

//not you can get rigth formatted connection string
var connectionString = connectionStringBuilder.ConnectionString;
Sign up to request clarification or add additional context in comments.

Comments

1

It's not possible. Actually you don't need to do this, that's why it's not possible. Because you can change the other parts of web.config same like AppSettings.

ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString =
String.Format("Data Source={0};Initial Catalog={1};UserID={2};Password={3}",
 "server", "db", "ID", "Pass");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.