-3

I got a new PC and copied a windows app project I was working on to my new machine. I also copied the associated MDF file over. I dragged the MDF into my solution, I can see it in the Server Explorer, Data Sources, and SQL Server Object Explorer.

As soon as my code tries to access the database, I get the following exception:

Cannot open database \"MyApp.Classes.NorthwindContext\" requested by the login. The login failed.\r\nLogin failed for user 'NEW-PC\Julien'.

The connection string:

connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=B:\MyApp\MyApp.Classes.NorthwindContext.mdf;Integrated Security=False"

I have attempted to put my username and password into the connection string to no effect, like so:

connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=B:\MyApp\MyApp.Classes.NorthwindContext.mdf;Integrated Security=False;User ID=NEW-PC\Julien;pwd=mypwd"

Some googling says that my login needs to in the Logins folder of the Security folder in the SQL Server Object Explorer, which it is.

What do I need to do to allow my code to read this database?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Julien
  • 212
  • 1
  • 18
  • 53

3 Answers3

2

First you should try to enable integrated security in your connection string. That will use your windows credentials to log to SQL database.

connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=B:\MyApp\MyApp.Classes.NorthwindContext.mdf;Integrated Security=True;"

smiech
  • 725
  • 4
  • 8
1

My guess is since you have moved database from one server to another, even though there is a user Julien in the database and the server also has a login Julien but the user Julien has been orphaned.

You will need to map this user to a login on this new machine/server. You can do something like

USE [DB_Name]
GO
ALTER USER JULIEN WITH LOGIN = JULIEN   --<-- this should be a valid login
GO

This will map this orphaned user to a login on this new server.

M.Ali
  • 67,945
  • 13
  • 101
  • 127
0

The problem was that i need to use

Data Source=.\SQLEXPRESS

simple as that

Julien
  • 212
  • 1
  • 18
  • 53
  • 1
    That is for attaching to the SQL Server default instance, the previous one was for connecting to a LocalDB instance. The problem was that the LocalDB instance did not have the login defined for it (within the master DB), the reason that moving from LocalDB to the default instance worked for you is a. You have it installed, b. You already defined that login within that instance. – Danny Varod Jan 17 '15 at 19:24
  • @DannyVarod I think you should change your comment to an answer, the OP cant award the bounty to himself AFAIK – Jeremy Thompson Jan 19 '15 at 04:20