質問

Full error below:

Error 1 SQL01268: .Net SqlClient Data Provider: Msg 1834, Level 16, State 1, Line 1 The file 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA\testdatabase.mdf' cannot be overwritten. It is being used by database 'testdatabase'. SchemaCompare5 25 0

I read about this on some forums and quite a few people were getting this and supposedly for some it had to do with parameterising the file path name to the db etc. or ticking "ignore file names and path for files and log files" prior to doing the comparison - this I have tried to no avail.

Someone else who has the same/similar issue: http://social.msdn.microsoft.com/Forums/en/vstsdb/thread/5a8b8c52-adb4-4a5a-95ed-09ad22bacf60

Basically for me I seem to get this error irrespective of which databases I am using for target and source. Say even if I create a new database with one table and another database with no tables and different name and try to update the schema of the database with no tables using the db with the single table it still gives me the error. Almost like SQL server express has gone nuts. I remember using the schema comparison tool before with no trouble. All db connections were created, tried many ways to do this to no avail ie: pointing to copy of *.mdf db in another folder or deleting things from the DATA folder in mysql directory in program files etc.

Also believe I read someone had solved a similar issue be deleting some files the scheme comparison tool creates, think they were *.sql type not sure which ones though.

役に立ちましたか?

解決

The problem arises because the database files already exist.

Try the below within the Visual Studio database project.

Create the schema comparison.

Go to menu: Data > Schema Compare > Export to > Editor

Once the script has been created delete the alter database commands that add the physical files. Then create a connection, switch to SQLCMD mode (making sure you have focus on the script) and execute the script.

To switch to SQLCMD mode access: Data > Transact-SQL Editor > SQLCMD Mode

他のヒント

If the target DB already exists, just delete through Management Studio first before you deploy for the first time.

I had already created the database manually through SQL Server Management Studio when I was establishing the original connection when creating the Database Project via the SQL Server 2008 Wizard in VS. It wouldn't allow me to continue until it could detect that the database existed. Then once I got to the Deploy step for the first time, it threw the same error as above. I just went into Management Studio and deleted the DB, then tried to deploy and it worked fine. Interestingly, it's deploying every time now without me having to go in and delete it every time.

      RESTORE DATABASE B FROM DISK = 'A.bak'
      WITH MOVE 'DataFileLogicalName' TO 'C:\SQL Directory\DATA\B.mdf',
      MOVE 'LogFileLogicalName' TO 'C:\SQL Directory\DATA\B.ldf',
      REPLACE     ---> Needed if database B already exists
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top