简体   繁体   中英

Do I have to connect a SQL Server database in Visual Studio to use it in C# project?

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:

在此处输入图像描述

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

在此处输入图像描述

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?

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.

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM