开发者

Cannot attach .mdf file on SQL Server

I've decided to upgrade my SQL Server 2008 R2 Express to the full trial version, and before unistall, I've take offline some of databases on that Express server, and detached all DB.

As I've planned, af开发者_运维百科ter unsinstall of the Express version and then I installed the Full trial version, the only thing that should be necessary is to attach the .mdf files and the job is almost done.

What happens is that mdf files that I've taken offline now I cannot attach to server!!!

Does anyone know how to resolve that?


Okay, two suggestions that might help to resolve this error:

  • Run SSMS (SQL Server Management Studio) as Administrator and try to attach
  • Try to manually attach the database using sp_attach_db

Manually attaching a database file:

exec sp_attach_db @dbname=N'<Database name>', @filename1=N'<MDF path and file name>', @filename2=N'<Log path and file name>'


While doing database update using code-first migrations in ASP.Net MVC, came across the strange exception and details are as follows,

Issue back ground details, 1. Manually deleted auto created ".mdf" file from App_Data folder using Visual Studio. 2. Executed update-database in package manager console. Then got the below exception,

System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file 'E:\Backup\Practice\MVC4\DotNetExamples\DotNetExamples\App_Data\DotnetExamples.mdf' as database 'DotnetExamples'.

Solution:

If you delete the DB file, it still stays registered with SqlLocalDB. Sometimes it fixes it by deleting DB. We can do this from the command line.

Open the "Developer Command Propmpt for VisualStudio" under your "Start/Programs menu->All Programs->Visual Studio 2012->Visual Studio Tools"

Run the following commands:

sqllocaldb.exe stop v11.0

sqllocaldb.exe delete v11.0

Please have the .mdf file and SSMS instance of the Database Deleted. Now execute "update-database" command from package manager console and it will create database for you without any obstacles.

Source

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜