Search code examples
c#sql-serverbackupattachmentlocaldb

c# SQL) unable to open physical file(MS localdb .mdf) after attaching the .mdf file which was created in another computer(server)


I'm developing Windows Desktop appication(C# WPF) with Microsoft localdb(.mdf) and I want that users of my software can carry their localdb (.mdf) file when they move to other places(computers). The localdb (.mdf) file is created on the first computer.

To test my application, I copied my localdb file from computer A to computer B and attached with below code successfully.

string attach_greendbnameQuery = string.Format(@"EXEC sp_attach_db @dbname = N'greendb_{0}', @filename1 = N'{1}\greendb_{0}.mdf'", textBoxGreenLogin.Text, Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString());
SqlCommand attach_greendbnamecomm = new SqlCommand(attach_greendbnameQuery, check_datadbinmasterConn);

attach_greendbnamecomm.ExecuteNonQuery();

And I could read and write data into the moved localdb file. However, when I run backup command on this database, exception occurs like, 'Unable to open physical file, Operating system error 32, process cannot open the file because the file is in use by another process'

If I enter Server Management Studio- Security-KayLee-PC\KayLee- User Mapping, the users of all other localdb are 'dbo' but only the user of manually moved database is KayLee-PC\KayLee and only 'public' is checked and all other database roles are not checked including db_owner. (I always start(login) Windows(O/S) with KayLee-PC\Kaylee account) I tried to make all roles checked to database roles even to server roles but failed. Even, I tried to drop the user KayLee-PC\KayLee through below code but the exception message show as,

'User 'KayLee-PC\KayLee' does not exist in current database'

If I click the database in Server Management Studio, the current database status is not changed to clicked database and subtree nodes(like Table, Security and so on) are not displayed with message 'cannot access to the database' eventhough if I click other databases, the current database status is changed to clicked database.

Also, I tried to change the owner of the localdb(database) through below code but it seems the execution failed with result '-1' with 'sa' and when trying with 'KayLee-PC\KayLee' which I always login to Windows O/S, error message is like 'KayLee-PC\KayLee is already an owner(exist) of this database'

SqlConnection dbownerchange_Conn = new SqlConnection();
dbownerchange_Conn.ConnectionString = dbownerchange_ConnectionString;
dbownerchange_Conn.Open();

SqlCommand dbownerchange_comm = new SqlCommand();
dbownerchange_comm.Connection = dbownerchange_Conn;
dbownerchange_comm.CommandText = "EXEC sp_changedbowner 'sa'";

dbownerchange_comm.ExecuteNonQuery();
dbownerchange_Conn.Close();

Simply, If we need to move(copy) Microsoft localdb file to another place(computer), how can I do this successfully? Must we detach before move the localdb file? If so, I'm worried there're always people who don't follow guideline by running detach function.


Solution

  • I've tried several scenarios to understand how SQL server is working. The conclusion is we need to detach first and re-attach to same or different machine(computer). Then, I could have been successful to move to another computer.

    However, a single process of backup and restoring localdb to another machine(computer) doesn't work. Furthermore, if we detach localdb(database file .mdf) first, SQL server no more recognizes the localdb database and we cannot run backup command for the localdb.

    Conclusively and simply, if we want to move localdb (microsoft database .mdf file) to other local server(computer, machine), we're needed to just detach the localdb from master database in computer A and copy the files and re-attach to another master database of computer B.

    Hope this helps someone else..