简体   繁体   English

连接字符串问题:System.Data.SqlClient.SqlException (0x80131904) - 无法连接到 SQL Server Management Studio

[英]Connection String Issue: System.Data.SqlClient.SqlException (0x80131904) - Unable to connect to SQL Server Management Studio

I am trying to connect Visual Studio 2017 to SQL Server 2016 Express using the following code in my app.config:我正在尝试使用 app.config 中的以下代码将 Visual Studio 2017 连接到 SQL Server 2016 Express:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
  </entityFramework>

  <connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
 </configuration>

I created a sample repository to test in my program.cs file我创建了一个示例存储库以在我的 program.cs 文件中进行测试

using System;
using System.Collections.Generic;
using System.Data.Entity;
sing System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
public class Post
{
    public int Id { get; set; }
    public DateTime DatePublished { get; set; }
    public DateTime DateEdited { get; set; }
    public string Title { get; set; }
    public string Body { get; set; }
}

public class BolgDbContext : DbContext
{
    public DbSet<Post> Posts { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
    }
}
}

I installed Entity Framework into my Console Application using the following command in the Package Manager Console我在包管理器控制台中使用以下命令将实体框架安装到我的控制台应用程序中

PM>install-package EntityFramework -version 6.1.3

Then I enable Migration and create the first migration:然后我启用迁移并创建第一个迁移:

PM>enable-migrations
PM>add-migrations CreatePost
PM>update-database

after submitting update-database command I get following error message:提交更新数据库命令后,我收到以下错误消息:

System.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

ClientConnectionId:00000000-0000-0000-0000-000000000000
Error Number:-1,State:0,Class:20
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

So my Visual Studio is not able to connect to my SQL Server Management Studio to create a database.所以我的 Visual Studio 无法连接到我的 SQL Server Management Studio 来创建数据库。

If this work right, my Code First console project should be able to use a Sql instance instead of LocalDb instance to create and update a database from Visual Studio.如果这项工作正常,我的 Code First 控制台项目应该能够使用 Sql 实例而不是 LocalDb 实例从 Visual Studio 创建和更新数据库。 ### I have searched google for solutions for hours last night. ### 我昨晚在谷歌上搜索了几个小时的解决方案。 Some suggest that could firewall issues, that I should create a port 1433 and enable TCP/IP in the Sql Server Configuration Manager to allow VS connecting to SQL.有人建议可能是防火墙问题,我应该创建一个端口 1433 并在 Sql Server 配置管理器中启用 TCP/IP 以允许 VS 连接到 SQL。 I have all those solutions, but none of them worked for my test project.我有所有这些解决方案,但没有一个适用于我的测试项目。

If anyone out there have an answer, please help.如果有人在那里有答案,请帮助。

Many thanks!非常感谢!

Further technical details更多技术细节

Target Framework: .NET Framework 4.5 Operating system: IDE: (eg Visual Studio 2017 15.4)目标框架:.NET Framework 4.5 操作系统:IDE:(例如 Visual Studio 2017 15.4)

Pretty sure by default EF looks for DefaultConnection connection name on your web.config.很确定默认情况下,EF 会在您的 web.config 上查找DefaultConnection连接名称。 Someone can correct me if I'm wrong.如果我错了,有人可以纠正我。

You need to override the connection name via :base() on your DbContext class.您需要通过DbContext类上的:base()覆盖连接名称。

public class BolgDbContext : DbContext
{
    public BolgDbContext() : base("name=BlogDbContext") { }
    public DbSet<Post> Posts { get; set; }
}

FYI, typo BolgDbContext is intentional, copied pasted from your code...仅供参考,错字BolgDbContext是故意的,从您的代码中复制粘贴...

I found solution as Following:我找到了以下解决方案:

<connectionStrings>
<add name="BlogDbContext" connectionString="Data Source=DESKTOP-I3K90LQ\SQL2016;Initial Catalog=CodeFirstDemo;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=localhost; Integrated Security=True; MultipleActiveResultSets=True"/>
</parameters>
</defaultConnectionFactory>

<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

So glad I found this and your solution!很高兴我找到了这个和你的解决方案! I'm just posting here to describe how I got the same problem and a bit more detail on how to fix it.我只是在这里发帖来描述我如何遇到同样的问题以及如何解决它的更多细节。

The same in VS2019, with EntityFramework and Update-Database.在 VS2019 中也是如此,使用 EntityFramework 和 Update-Database。 The same error message, ie A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).相同的错误消息,即A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server). A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server).

I was following a very good tutorial.我正在学习一个非常好的教程。 It had connectionString="Data Source=.;Initial Catalog=FriendOrganizer;Integrated Security=True".它有connectionString="Data Source=.;Initial Catalog=FriendOrganizer;Integrated Security=True". This had been working for half of the tutorial, and Update-Database had created and seeded the DB, so I knew the DB was accessible.这在本教程的一半中一直有效,并且 Update-Database 已经创建并播种了 DB,所以我知道 DB 是可以访问的。

After applying some changes in the tutorial it stopped working.在教程中应用一些更改后,它停止工作。 I deleted the DB and Migrations, recreated the Migrations How to delete and recreate from scratch an existing EF Code First database , but it sill didn't work, with Update-Database producing this error, after a timeout.我删除了数据库和迁移,重新创建了迁移如何从头开始删除和重新创建现有的 EF Code First 数据库,但它仍然不起作用,更新数据库在超时后产生此错误。

Using the flag '-Verbose' assisted somewhat in tracking it down, but I needed this answer!使用标志“-Verbose”在某种程度上有助于追踪它,但我需要这个答案! I went to the Sql Server Object Explorer , right-clicked on the first '(localdb)\\MSSQLLocalDB...', and got the Connection string property.我转到Sql Server Object Explorer ,右键单击第一个 '(localdb)\\MSSQLLocalDB...',并获得Connection string属性。 I copied this and replaced it in the connectionStrings in the App.config for the data access project.我复制了这个并在数据访问项目的App.config中的connectionStrings中替换了它。 Then Update-Database worked!然后更新数据库工作! (When I ran the app it timed out on the connection, as I needed to do the same in the Startup Project App.config , but that may not apply in all cases, depending on whether you have two App.configs ). (当我运行应用程序时,它在连接上超时,因为我需要在启动项目App.config执行相同的操作,但这可能不适用于所有情况,具体取决于您是否有两个App.configs )。

For my db, FriendOrganizerDb the connection string is...对于我的数据库, FriendOrganizerDb ,连接字符串是...

    <connectionStrings>
    <add name="FriendOrganizerDb"
         connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=FriendOrganizer;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

FriendOrganizer is the DB for the tutorial I was following - and highly recommend! FriendOrganizer是我遵循的教程的数据库 - 强烈推荐! ( Building an Enterprise App with WPF, MVVM, and Entity Framework Code First ) 使用 WPF、MVVM 和 Entity Framework Code First 构建企业应用程序

暂无
暂无

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

相关问题 不规则:SQL连接错误:System.Data.SqlClient.SqlException(0x80131904) - Irregular: SQL Connection Error: System.Data.SqlClient.SqlException (0x80131904) SqlException:System.Data.SqlClient.SqlException(0x80131904) - SqlException: System.Data.SqlClient.SqlException (0x80131904) MVC 应用程序 System.Data.SqlClient.SqlException (0x80131904): - MVC App System.Data.SqlClient.SqlException (0x80131904): c#System.Data.SqlClient.SqlException(0x80131904) - c# System.Data.SqlClient.SqlException (0x80131904) “重置连接”是什么意思? System.Data.SqlClient.SqlException (0x80131904) - What does “Resetting the connection” mean? System.Data.SqlClient.SqlException (0x80131904) System.Data.SqlClient.SqlException (0x80131904)) 从字符到字符串的转换数据或时间 - System.Data.SqlClient.SqlException (0x80131904)) conversion data or time from character to string 错误:System.Data.SqlClient.SqlException(0x80131904):将数据类型nvarchar转换为数值时出错 - Error:System.Data.SqlClient.SqlException (0x80131904): Error converting data type nvarchar to numeric System.Data.SqlClient.SqlException (0x80131904): '@med_data' 附近的语法不正确 - System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near '@med_data' 包失败,因为 System.Data.SqlClient.SqlException (0x80131904): &#39;s&#39; 附近的语法不正确 - Package fails because System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near 's' 尝试将数据发送到数据库时出现System.Data.SqlClient.SqlException(0x80131904) - System.Data.SqlClient.SqlException (0x80131904) when trying to send data to Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM