1

As per reference (Modifying connection string with variables), my App.config file line of interest:

<connectionStrings>
    <add name="MagiqDatabaseEntities" 
         connectionString="metadata=res://*/UnPalangiModel.csdl|res://*/UnPalangiModel.ssdl|res://*/UnPalangiModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source={0}\{1};initial catalog=MagiqDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
         providerName="System.Data.EntityClient" />
</connectionStrings>

(Note {0} and {1})

I have the following in my .cs file:

string connectionString = string.Format(ConfigurationManager.ConnectionStrings["MagiqDatabaseEntities"].ConnectionString, "HPPC", "NEWINSTANCE");

The server is HPPC\NEWINSTANCE

As per reference (Can't make a connection string in c#), I thought the backslash could be a problem. I have tried double backslash, but I still can't establish a connection.

The connection definitely works when data source is hardcoded to HPPC\NEWINSTANCE.

Thank you.

Community
  • 1
  • 1
taylorswiftfan
  • 1,371
  • 21
  • 35
  • Set a breakpoint and debug, what string is ConfigurationManager.ConnectionStrings["MagiqDatabaseEntities"].ConnectionString actually returning? Does it return a string with the {0}\{1} that we'd expect? – Michael Nov 05 '16 at 21:08

1 Answers1

1

Why not just simply set Data Source = {0}

And in your C# code do this:

string connectionString = string.Format(
    ConfigurationManager.ConnectionStrings["MagiqDatabaseEntities"].ConnectionString, 
    string.Concat("HPPC", @"\", "NEWINSTANCE"));
CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • Thank you for the suggestion. Debugger reveals: "metadata=res://*/UnPalangiModel.csdl|res://*/UnPalangiModel.ssdl|res://*/UnPalangiModel.msl;provider=System.Data.SqlClient;provider connection string=\"data source=HPPC\\NEWINSTANCE;initial catalog=MagiqDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework\"" – taylorswiftfan Nov 05 '16 at 21:13
  • Good suggestion, of course the concatenation could be completely unnecessary, unless they need the server and instance names to be modular. – Michael Nov 05 '16 at 21:13
  • @Michael Ineed. – CodeNotFound Nov 05 '16 at 21:17
  • @taylorswiftfan what do you mean by debugger reveals? Did it work for you? – CodeNotFound Nov 05 '16 at 21:17
  • Sorry, should have clarified - No it did not work for me. – taylorswiftfan Nov 05 '16 at 21:18
  • @taylorswiftfan Where do you stuck on with this solution? – CodeNotFound Nov 05 '16 at 21:19
  • The problem being the program still cannot talk to the database. – taylorswiftfan Nov 05 '16 at 21:20
  • @taylorswiftfan You should be able to troubleshoot this. You said hardcoding the server\instance into the connectionstring works fine? So then do that, set a breakpoint and debug. See what you get. Then compare that to what u are getting when you use string.format(). – Michael Nov 05 '16 at 21:24