0
votes

This is my connection string in App.config

<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True;
         User ID=sa;password=password-1;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>

</connectionStrings>

     ServerConnection connection = new ServerConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);

                    Server sqlServer = new Server(connection);

                    Database db = sqlServer.Databases[strDBName];

Exception : Failed to connect to server Data Source=PC210090\SQLEXPRESS; Initial Catalog=master;Persist Security Info=True; User ID=sa;password=password-1;multipleactiveresultsets=True; .

1
Have you checked whether you are able to connect to the database via Management Studio using same credentials? - Sagar Shirke

1 Answers

0
votes

What are you trying to do, not sure but change your connection string

connectionString="Data Source=PC210090\\SQLEXPRESS;

Initial Catalog=master;; don't connect to master DB rather provide a user DB name like Sample.

<connectionStrings>
<add name="CostAllocationEntities2" connectionString="Data Source=PC210090\\SQLEXPRESS; Initial Catalog=Sample;Persist Security Info=True; User ID=test;password=test;multipleactiveresultsets=True; " providerName="System.Data.SqlClient"/>
</connectionStrings>

In your code behind

Once you have the connection string create a instance of SqlConnection like

SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CostAllocationEntities2"].ConnectionString);

Open the connection

connection.Open();