简体   繁体   English

我是否必须在 Visual Studio 中连接 SQL 服务器数据库才能在 C# 项目中使用它?

[英]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.我在 SQL Server Management Studio 中创建了一个 SQL Server 数据库。 Now I need to connect to it in my Visual Studio 2019 C# project.现在我需要在我的 Visual Studio 2019 C# 项目中连接到它。

Is it essential that I have to connect to the database in Visual Studio 2019?我必须连接到 Visual Studio 2019 中的数据库吗? ( Tools >> Connect to database menu)? Tools >> Connect to database菜单)?

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:原来,我不得不以管理员身份重新启动 Visual Studio,修复了它,但接下来我得到这个文件正在使用的错误:

在此处输入图像描述

The solution to this apparently is to detach my database from SQL Server Management Studio?解决方案显然是将我的数据库与 SQL Server Management Studio 分离?

Does this mean I have to detach the database to connect to Visual Studio to connect to it?这是否意味着我必须分离数据库才能连接到 Visual Studio 才能连接到它? Do I have to attach it again in SQL Server Management Studio?我是否必须在 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?我可以只连接到数据库而不在 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.我真正需要的是连接字符串,但显然我只能在 Visual Studio 中连接它而不是直接从 SQL Server Management Studio 连接它。 Is there a way I get connection string from the SQL Server Management Studio 18?有没有办法从 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.就像 Dale K 评论的那样,我不需要在 Visual Studio 中连接,并且通过谷歌搜索,我能够找到这种方式来获取连接字符串。

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).首先创建一个扩展名为 .udl 的文件(任何名称,系统上的任何位置)。 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.如果您没有密码,请确保 select 收音机为红色。
  3. Enter the database name (path not required).输入数据库名称(不需要路径)。

Neat thing is you can hit the test connection to make sure it connects.整洁的是你可以点击测试连接以确保它连接。

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Visual Studio(C#)中将ASP.NET项目与SQL Server数据库表连接 - How to connect ASP.NET project with SQL Server database table in Visual Studio (C#) 使用C#连接到SQL Server 2012数据库(Visual Studio 2012) - Connect to SQL Server 2012 Database with C# (Visual Studio 2012) C#将数据库连接到Visual Studio项目 - C# Connect database to Visual Studio project 为什么使用Visual Studio 2013 + Microsoft SQL Server 2017 Developer连接数据库有空白页 - Why use Visual Studio 2013 + Microsoft SQL Server 2017 Developer to connect Database have blank page 如何将Oracle数据库连接到Visual Studio C#项目 - How to connect Oracle Database to Visual Studio C# project 在尝试使用C#连接到Visual Studio 2010中的本地SQL Server数据库时遇到问题 - Having problems trying to connect to local SQL Server database in Visual Studio 2010 using C# 在 Visual Studio 2015 中使用 SQL 服务器数据库从 C# 项目创建安装文件 - Create setup file from C# project with SQL server database in Visual Studio 2015 如何在 Visual Studio 2019 中从 C# 连接到 SQL 服务器 - How to connect to SQL Server from C# in Visual Studio 2019 如何使用 Visual Studio 通过 C# 连接到 SQL 服务器实例 - How to connect to a SQL Server instance with C# using Visual Studio 如何在Visual Studio中的其他项目中使用SQL Server数据库项目? - How to use SQL Server Database Project in other Projects in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM