如何将SQL Server Express数据库连接到VS 2017 [英] How to connect sql server express database to VS 2017

查看:377
本文介绍了如何将SQL Server Express数据库连接到VS 2017的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直遇到问题,只是尝试使用我在Sql Server Express中创建的一个表建立从Visual Studio 2017 Express中的c#程序到小型本地数据库的连接.我想将其作为数据源连接到我的解决方案(在c#中)的Entity Framework中.我已经在MSDN和该网站上搜索了几天,但还没有找到解决该问题的方法.

I keep running into issues just trying to make a connection from my c# program in Visual Studio 2017 Express to a small local database with one table I created in Sql Server Express. I would like to connect it as a data source within Entity Framework in my solution (which is in c#). I've searched MSDN and this site for days but haven't found a way to solve this.

现在的问题是,当我通过实体数据模型向导(现有数据库)并建立新连接时,选择"Microsoft SQL Server数据库文件(SqlClient)"并浏览到我的数据库-测试连接产生错误:

The issue now is that when I go through the Entity Data Model Wizard (existing database) and go to establish a New Connection, select "Microsoft SQL Server Database File (SqlClient)" and browse to my database -- the Test Connection generates the error:

无法打开物理文件"C:\ Program Files \ Microsoft SQL Server \ MSSQL13.SQLEXPRESS \ MSSQL \ DATA \ MyDatabase.mdf". 操作系统错误5:"5(拒绝访问.)". 尝试为文件C:\ Program Files \ Microsoft SQL Server \ MSSQL13.SQLEXPRESS \ MSSQL \ DATA \ MyDatabase.mdf附加自动命名的数据库失败. 存在具有相同名称的数据库,或者无法打开指定的文件,或者该文件位于UNC共享上.

Unable to open the physical file "C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\MyDatabase.mdf". Operating system error 5: "5(Access is denied.)". An attempt to attach an auto-named database for file C:\Program Files\Microsoft SQL Server\MSSQL13.SQLEXPRESS\MSSQL\DATA\MyDatabase.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.

我以管理员身份运行Visual Studio(这是一个先前的问题,即使没有提示我没有打开数据库文件权限的错误消息,我也无法浏览到数据库).

I'm running Visual Studio as Administrator (this got past a prior issue that wouldn't even let me browse to the database without prompting an error message that I didn't have permission to open the database file).

无论我是否以管理员身份运行数据库,都可以在Visual Studio的SQL Server对象资源管理器中查看数据库,其表及其数据.不知道这是否有意义. 我是这一切的新手,对不起,如果我错过了明显的内容.

I can view my database, its table, and its data in the SQL Server Object Explorer inside Visual Studio - regardless of whether I'm running it as Administrator or not. Not sure if that is significant. I'm new to all this, so sorry if I am missing something obvious.

推荐答案

弄清楚了我的问题: 在Visual Studio的添加新数据源"向导中,我一直选择的选项是"Microsoft SQL Server数据库文件",因为其描述是使用此选择将数据库文件附加到本地Microsoft SQL Server实例(包括Microsoft SQL Express) )使用适用于SQL Server的.NET Framework数据提供程序."

Figured out my issue: in Visual Studio's "Add New Data Source" wizard, the option I kept choosing was "Microsoft SQL Server Database File", because its description is "Use this selection to attach a database file to a local Microsoft SQL Server instance (including Microsoft SQL Express) using the .NET Framework Data Provider for SQL Server."

但是,我不确定为什么,这不是正确的选择.

However, and I'm not sure why, this was not the right selection.

我尝试选择其他".

然后在下一页上输入.\ SQLExpress"作为服务器名称(在connectionstrings.com/sql-server/上提到...感谢潜在客户@ ryguy72!)

Then on the next page input ".\SQLExpress" as the Server Name (mentioned on connectionstrings.com/sql-server/ ...Thanks for the lead @ryguy72!)

然后,在连接到数据库"下,弹出我的本地数据库列表,包括我已经使用SSMS创建的数据库列表.

Then, under "Connect to a database" my local list of databases popped up, including the one I had created already using SSMS.

然后,测试连接终于可以工作了!

Test Connection finally worked then!

这篇关于如何将SQL Server Express数据库连接到VS 2017的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆