-1

I created a SQL Server database in SQL Server Management Studio. Now I need to connect to it in my Visual Studio 2019 C# project.

Is it essential that I have to connect to the database in Visual Studio 2019? (Tools >> Connect to database menu)?

Because first I got this error that I don't have permission:

enter image description here

Turns out, I had to restart Visual Studio as Admin, that fixed it but next I get this error that file is in use:

enter image description here

The solution to this apparently is to detach my database from SQL Server Management Studio?

Does this mean I have to detach the database to connect to Visual Studio to connect to it? Do I have to attach it again in SQL Server Management Studio? Wonder if I will get error there then that it is being used elsewhere.

Can I just connect to the database without connecting it in Visual Studio?

What I really need is connection string but apparently I can only get that if I connect it in Visual Studio and not directly from SQL Server Management Studio. Is there a way I get connection string from the SQL Server Management Studio 18?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
zar
  • 11,361
  • 14
  • 96
  • 178
  • No you don't need to connect it in VS unless you want to take advantage of the build in database tooling. Google connections strings and you will see how simple they are and how easy they are to build by hand. – Dale K Dec 06 '20 at 07:29
  • The connection from visual studio to your database is just a convenience tool. You can always administer the database via a separate tool (e.g. SSMS). It's the connection between the c# application and the database which is important. – ADyson Dec 06 '20 at 07:50

1 Answers1

0

Like Dale K commented, I don't need to connect in visual studio and with that, googling, I was able to find this way how to get the connection string.

The answer is there but I am posting an image of the settings and recap. First create a file with .udl extension (any name, anywhere on the system). Now double click that in explorer and do the following:

  1. You do need to enter server name (it was not able to populate the list by itself).
  2. Make sure to select the radio in red if you don't have password.
  3. Enter the database name (path not required).

Neat thing is you can hit the test connection to make sure it connects.

enter image description here

zar
  • 11,361
  • 14
  • 96
  • 178