SQL Server Management Studio error 948 - sql-server

I tried to attach a database (.mdf) file and I got this error:
Attach database failed for Server '...\SQLINSTANCE'. (Microsoft.SqlServer.Smo)
Additional information:
An exception occured while executing a Transact-SQL statement or batch.
(Microfost.SqlServer.ConnectionInfo)
The database 'DBName' cannot be opened because it is version 782. This server supports version 706 or earlier. A downgrade path is not supported.
Could not open new database 'DBName'. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)
P.S. I have installed SQL Server 2012 Express, and database 'DBName' has been made in Visual Studio 2012.

If you have source control
Rollback the mdf file
Rollback the idf file (log file) to the same version
I had a similar problem and this solution fixed is.
If you do not have source control and you are using Code First
Backup the mdf and idf files
Delete the mdf and idf files in the solution explorer
Create a new database from project->add new item->data->SQL server database
In the package manager console write "Enable-migrations" then "Add-migration init" then "Update-database" to create your tables in the database.
For me, if things got too complicated, I would just recreate the project and copy the code :)

Related

Attaching a database to SQL Server (Management Studio) - Error 5

I am trying to attached a database from a previous company to a new SQL Server. The .mdf and .ldf files are both saved to my desktop.
When I try to attach the database, I get the following error:
I have attached this database before, with no problem.

Database already exists even after deleting the database files

I installed a trading software that uses pair specified and gives me details about it. For initiating the software I have to create a database that it uses to store and retrieve information from. Now I have deleted the database(mdf and ldf file) and now when I create it again it shows that database already exists error. Do I have to delete DSN or something?
I can do create database using another sql server instance, but I want to do on the same one.
I am clueless. Thanks in advance.
The database information is maintained by sql server express. So just deleting the file doesn't remove the database from sql server. You need to detach the database and then your logic should work. Try sql server management studio express 2008. Or try connect to sql server express programmatically and execute sp_detach_db permissions.
Source : MSDN
A Database is not only MDF and LDF files but also a record in the master database. So you should drop this record about your DB using DROP DATABASE command or through MSSQL Enterprise Manager.

Can't attach northwind database to sql server 2008 R2

When I try to I get the following error in SQL Server Management Studio:
TITLE: Microsoft SQL Server Management Studio
Attach database failed for Server 'AHAKEEM'.
(Microsoft.SqlServer.Smo)
ADDITIONAL INFORMATION:
An exception occurred while executing a Transact-SQL statement or
batch. (Microsoft.SqlServer.ConnectionInfo)
Unable to open the physical file "C:\SQL Server 2000 Sample
Databases\northwnd.mdf". Operating system error 5: "5(failed to
retrieve text for this error. Reason: 15105)". (Microsoft SQL Server,
Error: 5120)
This is a fresh version of Northwinds mdf which just came from Microsoft's installer.
Error 5120 is a sharing violation on the file you're opening. Try starting SQL Management Studio as Administrator and make sure that the db isn't attached already.
The error occurs when the mdf or ldf file is missing, if its an ldf we can recreate the same using the below listed scripts:
Method 1: To recreate all the log files
EXECUTE sp_attach_single_file_db #dbname = 'SAMPLEDB',
#physname = N'D:\MSSQL\DATA\SAMPLEDB.mdf'
GO
Method 2: If one or more log files are missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
(FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH_REBUILD_LOG
GO
Method 3: If only one file is missing, they are recreated again.
CREATE DATABASE SAMPLEDB ON
( FILENAME = N'D:\MSSQL\DATA\SAMPLEDB.mdf')
FOR ATTACH
GO
I tried to install Northwind and pubs Sample Databases for SQL Server 2000 and attach both databases in SQL Server 2014, and gave me an error because they were compatible version.
These are the steps to successful install the Sample Database in your SQL Server 2014:
After you download the Sample Databases in your PC, then open SQL Server 2014 Management Studio. After successful connection, your SQL Server instance should be listed in the Object Explorer.
Now under File Menu select Open > File.
Find the instnwnd.sql in your computer and select it. Click Open
SQL script should open in the main window.
Do not click on Execute yet. If you do, you will get the following error:
Could not find stored procedure ‘sp_dboption’.
Around the line 20, remove the following two lines:
exec sp_dboption 'Northwind','trunc. log on chkpt.','true' exec
sp_dboption 'Northwind','select into/bulkcopy','true'
Replace them with this line as shown below:
alter database Northwind set recovery simple
To attach 'pubs' database, repeat the step 2 above and this time open instpubs.sql file. Replace the sp_dboption as you did before with:
alter database Pubs set recovery simple
All that is left is to execute the script, so click on Execute in both scripts.
Note: the folder location for your sample database by default is "C:\SQL Server 2000 Sample Databases"

Cannot connect to the database

I have created a database in App_Data and I want to use this file in my project.
In the toolbar I grabbed the sqlDatasource. I clicked on Configure data source. I chose Microsoft SQL Server database file and said continue. I browsed for the file. I chose it then I clicked ok. The following error occurred:
I says the database myweb.mdf cannot be opened because it is version 661 and the server version is 662 and earlier: create database is aborted. The attempt to attached an auto-named database for the \app-data\myweb.mdf failed. A database with the same name exists or specified file cannot be opened or it is located on UNC share.
Sounds like you've upgraded to SQL Server 2008 R2 Whilst the database was created in SQL Server 2008, seems you need to upgrade the database using the database migration wizard
see this answer for a similar problem
Check this: SQL Server: Attach incorrect version 661
You'll have to either upgrade your 2008 SP2 instance to R2, or you have to copy out the data in that database into an 2008 database (eg using the data migration wizard, or something equivalent).

sql server mdf file database attachment

I'm having a bear of a time getting visual studio 2010 (ultimate I think) to properly attach to my database. It was moved from it's original spot to #MYAPP#/#MYAPP#.Web/App_Data/#MDF_FILE#.mdf. I have three instances of SQL Server running on this machine. I have tried to replace the old mdf file with my new one and cannot get the connectionstring right for it.
What I'm really trying to do is to just open a DB instance and run a DB create script. Then I can have a DB that was generated via my edmx (generate database from model) in Silverlight business application (c#)
Right now, when I go to Server Explorer in VS, choose add new connection, choose MS SQL Server Database File (SqlClient), choose my file location (app_data directory), use Windows authentication, and hit the Test Connection button, I get the following error:
Unable to open the physical file "". Operating system error 5: "5(Access Denied.)". An attempt to attach to an auto-named database for file"" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
The mdf file was created on the same machine by
Connecting to (local) in SQL Server management studio.
Getting a new query.
Pasting in the SQL from the generated ddl file.
Adding CREATE DATABASE [NcrCarDatabase]; GO; before the pasted SQL and
Executing the query.
I then
Disconnected from the DB in Management Studio.
Closed Management Studio
Navigated to the DATA directory for that instance
Copied the mdf and ldf files to my application's app_data folder.
I am now trying to connect to the same file inside visual studio.
I hope that gives more clarity to my problems :).
Connection string is:
Data
Source=.\SQLEXPRESS;AttachDbFilename=C:\SourceCode\NcrCarDatabase\NcrCarDatabase.Web\App_Data\NcrCarDatabase.mdf;Integrated
Security=True;Connect Timeout=30;User
Instance=True
I was getting "access denied" until I ran Visual Studio as an administrator. I then had to detach the database from MSSMS before I could get it into Visual Studio.
You mentioned that your MDF file was moved. Was the MDF properly attached to SQL Express after the move? See: How to: Attach a Database File to SQL Server Express.
Access denied might well mean that you already have an active instance that has opened the file prior to you doing it - and if you have three active instances, it is likely one of them that is the culprit.
You can try to connect to the different instances with Management Studio and see if you can find the base there. If yes, the detach it from the instance and re-try from Visual Studio. Or, if possible, try to shut down all three instances and then re-try from Visual Studio. If that works, restart the instances one by one to determine which of them is trying to get the file.
I've previously answered a similar question that might help.
Your Sql server service runs under a given account. You should determine the account through the service properties then change the db file security access to full access for that account.
Give Security to Full control for your application Folder and .mdf, .ldf files. Write click on the folder which contains .mdf,.ldf files
Go to property
click security in that
Click Full Control in allow.
and them proceed your process.

Resources