An iIssue with SQL database attachment via Visual Studio - sql-server

Me
An SQL newbie
My tools
VS Community 2017 v15.9.3 / C#
SQL Server 14.0.2002.14
SSMS 14.0...
What I want to do
I have a file, you say mydatabase.mdb, I am trying to create a connection to the database on SQL Server to access to it.
The database is not actually attached to SQL server, so I try to create my connection and attach it (correct?) using Server Explorer window on VS. I right click on "data connection" then choose "add connection"; in the "add connection dialog" I select the datasource "Database file of Microsoft SQL Server" and I give it the pathname of my file. Finally I hit "Test Connection".
The problem
On "Test Connection" I got this message:
The database 'mydatabase.mdb'cannot be opened because it is version 869. This server support version 852 and earlier.
Could not open new database 'mydatabase.mdb', CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file 'mydatabase.mdb' failed. A database with the same name exists, or specified file cannot be open or it is located on UNC share
My attempts
I connected the databse to SSMS. When I asked DB version to SSMS, I got: SQL Server 2016 (130). It is not congruent with MS error report. Anyway I changed database version to 2014, then I detached it from server and try again: same result.
Last note: if I create my connection on attached (by SSMS) database, and specifing directly the SQL server (not the file) as datasource, it seems to work. I could use this way, but I'd like to know the cause of the problem.
Can anyone help me?

You may have installed VS 2012, but you are using a SQL 2008 instance: Visual studio does not install SQL for you.
SQL Server 2008 databases are version 655. SQL Server 2008 R2 databases are 661. You are trying to attach an 2008 R2 database to an 2008 instance and this is not supported.
You need to upgrade your SQL instance to at least 2008R2 to open the file (but be aware that if you subsequently attach your existing DBs to the new instance, you won't be able to open then in SQL 2008 again either)

Your SQL Server version (the server that reported the error) is 2016, that is, it supports db version 852 and earlier.
The database that you try to attach is of version 869, this means it was created on SQL Server 2017.
You cannot attach it to an earlier version of SQL Server, so you need to upgrade your server to 2017 or install another instance of SQL Server, SQL Server 2017.
P.S. You CANNOT downgrade database version, i.e. when you said
I changed database version to 2014
it is not true.
Maybe you just change the compatibility level of database, but it's version is still 869.
Compatibility level means a version of cardinality estimator to use + some features, it has nothing to do with database version that refers to physical file format. Database version cannot be downgraded, only upgraded.
P.P.S.
I connected the databse to SSMS
You cannot "connect database to SSMS", you are connecting to server, and the database is also attached to server. And if you were able to attach this database to SQL Server, this means you have SQL Server 2017 already installed.
P.P.P.S. To detect your SQL Server version you shopuld execute this code: select ##version (from SSMS). It seems that you have at least 2 servers, 2016 and 2017 on your pc. You can verify this by opening SQL Server Configuration Manager, or you can use Windows Services to determine how many instances you have and what are their names. Then all you should do is to use the correct instance.
Here is the picture of my SQL Server instances, you can verify your instances by launching services.msc on your pc.

Related

Creating Database in Visual Studio 2015

I am trying to create a database in Visual Studio 2015 MVC setup. When I create a new file in the "App_Data" folder and select SQL Server Database, a popup comes up that says the following:
"Connections to SQL Server database files (.mdf) require LocalDB or
SQL Server Express to be installed and running on the local computer.
You may need to modify the setup and ensure Microsoft SQL Server Data
Tools is selected."
I have already installed SQL Express in my computer. But Visual Studio still shows the same popup. Can anyone please give me a step by step instruction on how to create a DB through Visual Studio 2015?
(My thought was if I clicked on new file->SQL server Database, then a popup would launch allowing me to input my data and name it. But I obviously am unable to do that).
Make sure that SQL Server is up and running. Open up SQL Server Management Studios (installed with SQL Server) and try and connect to the instance that you created (should be the same as your machine name). If it fails to connect, SQL Server might not be running.
To turn it on, open SQL Server Configuration Manager (just do a search and it'll show up), go to SQL Server Services, and turn on the database engine. You should then be able to connect to the database.

Issues restoring a Database in SQL Server 2014 Management Studio

I took a project where I have to do a few fixes to a website and I'm having several problems trying to import the DB from a backup to my local computer.
The message I got is the following:
ADDITIONAL INFORMATION:
System.Data.SqlClient.SqlError: The database was backed up on a server
running version 12.00.2000. That version is incompatible with this
server, which is running version 11.00.3000. Either restore the
database on a server that supports the backup, or use a backup that is
compatible with this server. (Microsoft.SqlServer.SmoExtended)
The thing is I'm running SQL Server 2014 Management Studio and it is a 12.2000.8 version so it should work, but it doesn't.
As a second option I tried to generate the script (as I can connect to the remote DB) but it also doesn't let me. It just gives an error somewhere in the middle of the creation of the script.
Any ideas what could I be missing or what can I do to fix this?
I'm working with win 8.1, VS 2014 & SQL Server 2014 Management Studio everything up to date.
Regardless of the version of management studio you're using, the version of the database instance sounds like it's 2012. If you create a backup with 2014, you won't be able to restore on a 2012 instance.

VS 2012 Database no longer working after opening in SQL Server Management Studio

I have a database that I have been working with in Visual Studio 2012. I had opened the database user SQL Server Management Studio for SQL Server 2012 so that I could backup the database. After backing up the database, I detached it from SQL Server Management Studio. Now I can not get the database to open in Visual Studio 2012. I get the following message:
The database 'H:\PBC\PBCSITE2012\APP_DATA\PBCDATA.MDF' cannot be opened because it is version 706. This server supports version 662 and earlier. A downgrade path is not supported.
Could not open new database 'H:\PBC\PBCSITE2012\APP_DATA\PBCDATA.MDF'. CREATE DATABASE is aborted.
An attempt to attach an auto-named database for file H:\PBC\PBCSite2012\App_Data\PBCData.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
Any ideas on what I did to cause this or what I can do to repair it?
Version 706 is a database file from Sql Server 2012
Version 665 is a database file from Sql Server 2008R2 (post SP1 ?)
You are attempting to open a later version of the database file than the current server version ( for example using 2008 r2 to open a 2012 database)
Check your connection strings to verify that connection is being made to the correct instance of the database server.

SQL Server version 612 , 655?

I have SQL Server 2008 and VS2008 installed on my computer and I am trying to run a web application I've created on a computer which has SQL Server 2008 and VS2008 and VS2010 beta 2 with its SQL Server Express.
I am getting an error with the database version that is
"...aspnetdb.mdf" cannot be opened because it is version 655. This server supports version 612 and earlier...."
Configuring the database in Visual Studio ->Tools -> Options -> Design-Time Validation Database is not working since I don't have the SQL Server version comes with VS 2010.
EDITED : I also edited aspnetdb and inserted a few more tables so I need the same aspnetdb
Is there any solution to convert this db ?
Also, after fixing this, I am looking forward to upload it on my hosting which has SQL Server 2005 DB and configure it in PLESK.
The SQL Server databases have a version specific format. Each SQL Server runtime version (SQL 2008 SP1, SQL 2008 RTM, SQL 2005 SP2, SQL 2005 SP1, SQL 2005 RTM etc) knows how to upgrade to its own verison. Unfortunately, once upgraded the database cannot be downgraded. So if your MDF was upgraded to version 655, it cannot be downgraded to any prior version. If you know you're going to deploy on SQL Server 2005, you must develop on SQL Server 2005 too. Not only that, but the actual build number of your development has to match the build number of the deployment (or at least be lower than that): the SP level and CU level must match.
You can a attach the 2008 version MDF you created to a SQL Server 2008, script out the database content, then import the script into a SQL Server 2005 database of the proper version.
I had this problem and when I changed my_computer_name\SQLEXPRESS with . to connect to server and it works and I could attach MDF file
may be you have both services run. (Mssql2008 and mssqlexpress) Maybe you have runing the mssqlespress running, that's the error that you have. Try to stop the mssqlserver express service and run mssql2008 services, and attach again your databases. I try this and works for me

Configure VS2008 to create SQL Server databases instead of SQL Server Express?

Probably asked and answered before, but difficult to search for.
In VS2008 when you right-click App_Data folder and create new database, it attempts to create a SQL Server Express database. Well I have SQL Server 2005 Standard installed and have thus uninstalled Express. How do I get VS2008 configured to know I want SQL Server databases (NOT Express) created?
SQL Server Express databases are SQL Server databases and vice-versa. While is true that the SQL Server 2008 database files format is different from SQL Server 2005 one, whthin the same version (2005, 2005 SP1, 2005 SP2, 2008 , 2008 SP1 etc etc) all SKUs (Express, Standard, Exnterprise etc) have all the same datbase format.
Yout Visual Studio tools are guiding you down the wrong path. You should not use the Solution Explorer to add a database to the App_Data folder. Instead you should use the Server Explorer tool (menu View/Server xplorer or press Ctrl+W,L) and connect to your SQL Server 2005 instance. Then use the Server Explorer tool to explore the database. To connect to the database from your solution, add a connection string to the web.config file.
While you can manage the database objects from the Server Explorer, thar is a horrible way to do it and will cause only pain on the long run. You should instead create deployment scripts with DDL statements and run those scripts when the solution is deployed. This way your database metadata is part of your source control and you can keep track of application database versions, see Version Control and your Database.

Resources