How to create a database in SSMS and connect to it in Visual Studio 2019

Andreas ss 726 Reputation points
2020-11-03T15:42:32.353+00:00

Hello!

  • SQL Server Management Studio
  • Visual Studio 2019 Preview
  • Microsoft SQL Server Enterprise Core 2017

This is the first time I do this and I am not sure of how to create a SQL database that I can use in Visual Studio 2019.
I will tell the steps I have done and please correct me on the way.

Step 1:
I have created a database and named it: "database1". As seen I run a Query to find out the exact path location of the database:
C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\database1.mdf
37232-n1.png

Step 2:
I am now trying to "Connect to Database" from Visual Studio 2019. As seen in the image below, I paste the Path to the database:
37146-n2.png

First I wonder if this is the correct way to do it?
Then, I wonder about the error messagebox that shows this message?

An attempt to attach an auto-named database for file "C:\Program Files\Microsoft SQL Server\MSSQL14.MSSQLSERVER\MSSQL\DATA\database1.mdf" failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

Thank you!

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,696 questions
0 comments No comments
{count} votes

Accepted answer
  1. tibor_karaszi@hotmail.com 4,311 Reputation points
    2020-11-03T16:32:18.86+00:00

    You have the wrong data source. VS tries to attach the database files into its own "internal" SQL Server, but that database file is already used by your installed SQL Server. Here's an image from my machine:

    37147-capture.png

    My machine name is TK. I have a named instance named A on that machine. I believe you have a default instance so insstead of TK\A, you just have the name of the machine where your SQL server is installed. And, of course pick the correct database name.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.