Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have created the setup file for a project (already have all of .mdf and .ldf files). When I install on my PC, it's OK. But If I bring to another PC, Install --> OK, but Run --> get the error

C#
Cannot open database "Standing" requested by the login. The login failed. Login failed for user "(name of PC)"



In source code, I use 3 databases with 3 connectionString like this:

C#
public void LoadDB()
{
    conDB = new SqlConnection();
    conDB.ConnectionString = @"Data Source=.\SQLEXPRESS;
                  AttachDbFilename=C:\EPL.mdf;
                  Integrated Security=True;
                  Connect Timeout=30;
                  User Instance=True";
    conDB.Open();

    conFix = new SqlConnection();
    conFix.ConnectionString = @"Data Source=.\SQLEXPRESS;
                  AttachDbFilename=C:\Fixtures.mdf;
                  Integrated Security=True;
                  Connect Timeout=30;
                  User Instance=True";
    conFix.Open();

    conStan = new SqlConnection();
    conStan.ConnectionString = @"Data Source=.\SQLEXPRESS;
                  AttachDbFilename=C:\Standing.mdf;
                  Integrated Security=True;
                  Connect Timeout=30;
                  User Instance=True";
    conStan.Open();
}


What should I do?
Posted

1 solution

1. If it's Microsoft SQL Server Express you should try the following:

Server=(localdb)\v11.0;Integrated Security=true;
AttachDbFileName=C:\MyFolder\MyData.mdf;


C#
public void LoadDB()
{
    conDB = new SqlConnection();
    conDB.ConnectionString = @"Server=(localdb)\v11.0;
                  Integrated Security=True;
                  AttachDbFilename=C:\EPL.mdf";
    conDB.Open();
}


This should work for you.

2. Do you use SQL server or Windows authentication ? If you're using SQL server authentication
in your connection string I believe that you should also provide: User ID=myUsername; Password=myPassword; :

C#
public void LoadDB()
{
    conDB = new SqlConnection();
    conDB.ConnectionString = @"Server=(localdb)\v11.0;
                  Integrated Security=True;
                  AttachDbFilename=C:\EPL.mdf;
                  User ID=myUsername;
                  Password=myPassword";
    conDB.Open();
}
 
Share this answer
 
Comments
Member 12035413 30-Dec-15 2:38am    
sorry but I'm using Windows authentication, can you help me?
Arthur V. Ratz 30-Dec-15 2:55am    
Yes, sure, I can help you to workaround this problem. Since you're using SQL server authentication you have to provide User ID=myUsername; Password=myPassword; in your connection string as it has been shown above. Also, make sure that your user account has an appropriate permision to manage the database object.
Arthur V. Ratz 30-Dec-15 2:57am    
if the solution that I've posted works for you, just let me know.
Member 12035413 30-Dec-15 3:53am    
It's not work. When I use your code, I get this error

Database 'C:\EPL.MDF" cannot be upgraded because it is read-only, has read-only files or the user does not have permissions to modify some of the files. Make the database or files writeable, and return recovery
An attemp to attach an auto-named database for file C:\EPL.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share
Arthur V. Ratz 30-Dec-15 6:23am    
1. Check the exact path to the database file EPL.mdf (It's recomended that you use the fully qualified path string just for testing purposes;

2. Try to either change permissions to the database by changing the attributes of a role the user belongs to, or try to convert the database by exporting the data base into plain SQL-file and than try to recreate it with the new permissions using that SQL-file;

That's all. If it works or not, just let me know. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900