3

Environment: Visual Studio 2017
.Net Framework 4.6.1

Update I apologize, I failed to mention that I was using a UnitTesting project to do an integration test. So it looks like I need the app.config in there as well, correct?

I am getting an unexpected value for connection string when using the following:

C# .Net Framework Library

string connStr = ConfigurationManager.ConnectionStrings["test123"].ConnectionString;

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="test123" connectionString="Data Source=.;Initial Catalog=AbcCompanyDb;Integrated Security=True;"/>
  </connectionStrings>
</configuration>

I did a search on Entire Solution and can't find any reference to

.\sqlexpress

Value of connection string during debug

{data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true}
Rod
  • 14,529
  • 31
  • 118
  • 230
  • What is `Data Source=.;`?? `.` is the correct name of your Data Server? – Ramesh Rajendran Feb 19 '18 at 05:34
  • Is it a Web application? If yes, then you need to check web.config and set the connection over there – Jayakrishnan Feb 19 '18 at 05:35
  • 1
    @RameshRajendran `.` points to `localhost` or `(local)` – John Woo Feb 19 '18 at 05:36
  • Please take a look at this discussion. https://stackoverflow.com/questions/20217518/sql-server-connection-strings-dot-or-local-or-localdb – Ramesh Rajendran Feb 19 '18 at 05:37
  • 1
    If I hardcode the connection string to the variable connStr the code works. This is not a web project. – Rod Feb 19 '18 at 05:42
  • I'd first recheck one more time that app.config your are looking at is correct. For example you need to look at YourAppName.exe.config, not at app.config file in your visual studio solution. If you are under VS debugger - you need to look at YourAppName.vshost.exe.config. – Evk Feb 19 '18 at 06:08
  • Please also go through with this link - https://stackoverflow.com/questions/42373392/c-sharp-configurationmanager-retrieves-wrong-connection-string-from-app-config – Jatin Parmar Feb 19 '18 at 06:11

6 Answers6

2

You need to add the connectionStrings section to your UnitTest Project's app.config.

Lawrence Thurman
  • 657
  • 8
  • 15
0

The value could be coming from a higher web.config (like the machine.config) although it is strange to be the same value test123 (you should get an error if you try to add a value that's already defined). You can clear any previously set values using <clear /> before you add the new value:

<connectionStrings>
    <clear />
    <add name="test123" connectionString="Data Source=.;Initial Catalog=AbcCompanyDb;Integrated Security=True;"/>
</connectionStrings>
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
0

. mean that the connection will be made using the named pipes (shared memory) protocol within the same machine (doesn't need to go through the TCP/IP stack)

You may need this below topics as well

SQL Server Connection Strings - dot(".") or "(local)" or "(localdb)"

Change a Sql-Server (express) from a named instance to localhost?

Can't connect to localhost, but can with computer name in SQL Server 2008

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
0

SQLEXPRESS is a reference to your local instance of SQL Server Express, and using '.' as the server name is equivalent to declaring 'use my local SQL server'.

If SQL Server Express is registered as your default local SQL server, then internally a reference in your connection string to '.' will be understood as a reference to 'SQLEXPRESS'. Hence the value that's being generated for your server name when you debug.

Chris Halcrow
  • 28,994
  • 18
  • 176
  • 206
0

aspnetdb.mdf is the default database when using ASP.NET Application Services. You will not find it in your code.

try placing your connection string in the web.config file

<configuration>
    <connectionStrings>
        <add name="test123" connectionString="Data Source=.;Initial Catalog=AbcCompanyDb;Integrated Security=True;"/>
    </connectionStrings>
</configuration>

if you are using SQL Express, use ".\SQLEXPRESS" for the Data Source

<configuration>
    <connectionStrings>
        <add name="test123" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=AbcCompanyDb;Integrated Security=True;"/>
    </connectionStrings>
</configuration>
MEC
  • 1,690
  • 1
  • 17
  • 23
0

So I failed to mention I was performing an integration test through a unit test project. I mistakenly put the app.config in my data access project instead of the unit test project.

Rod
  • 14,529
  • 31
  • 118
  • 230