Visual Studio 代码映射:无法连接到指定的数据库 [英] Visual Studio Code Map: Unable to connect to the specified database

查看:62
本文介绍了Visual Studio 代码映射:无法连接到指定的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于此错误,我无法在 Visual Studio 中使用代码映射:

<块引用>

无法连接到指定的数据库.

尝试连接到数据库时发生异常以下连接字符串:数据Source=(LocalDB)\v11.0;AttachDbFilename=;Initial目录=主;集成安全=真;登记=假;异步Processing=True;MultipleActiveResultSets=True;连接超时=30.

检查指定的 SQL Server 实例是否存在,并且服务是运行.

我已经阅读了同样的问题:

  1. 在服务器名称字段中,如果您没有看到列出的服务器名称,请单击浏览更多...

  1. 单击数据库引擎旁边的 + 号,您将看到您的服务器名称.单击服务器名称并选择它.现在连接到您的 SQL Server.

现在连接到您的 SQL Server.

第 2 步.startup.cs 中添加服务在 ConfigureServices(IServiceCollection services){ 将你的 sql connectiono 服务放在这里 } 方法

//DatabaseContext 来自 DatabaseContext.csservices.AddDbContext(options =>options.UseSqlServer(Configuration.GetConnectionString("sqlConnection"))//sqlConnection 来自 appSettings connectionString);

I can't use Code Map in Visual Studio due to this error:

Unable to connect to the specified database.

An exception occurred attempting to connect to a database using the following connection string: Data Source=(LocalDB)\v11.0;AttachDbFilename=;Initial Catalog=master;Integrated Security=True;Enlist=False;Asynchronous Processing=True;MultipleActiveResultSets=True;Connect Timeout=30.

Check that the specified SQL Server instance exists and the service is running.

I've read this questions with the same problem:

Code Map not working in VS2012

visual studio 2012 ultimate new dependency graph error

1) I've installed SQL Server Data Tools for VS2012

2) I've updated VS, and re-installed SQL Server component (in a default uninstall/modify/repair menu)

3) Then I successfully stopped and deleted local database (like in 2nd question):

sqllocaldb stop "v11.0" -k

sqllocaldb delete "v11.0"

But failed to create one:

sqllocaldb create "v11.0"

Creation of LocalDB instance "v11.0" failed because of the following error:

The specified LocalDB version is not available on this computer.

But creating and starting another version succeeded:

sqllocaldb create "v12.0"

LocalDB instance "v12.0" created with version 12.0.2000.8.

sqllocaldb start "v12.0"

LocalDB instance "v12.0" started.

Now I'm confused what is actually wrong. After each step I restarted VS and tried to use diagrams - but nothing changed.

How to fix the problem?

解决方案

Have you tried changing your database name? You can use something like (Look at your appsettings.json file or web.config file)

<add name="DefaultConnection" connectionString="Server=YourDatasource;Database=DatabaseNameAsYouWish;Integrated Security=SSPI" providerName="System.Data.SqlClient" />

If you use SQL Management Studio, you will find the Data Source Connection string while you are connecting to the database. Copy and paste it, then try again. If it works, let me know, please.

Edit: Setup Connection String for ASP.NET Core 5 Web API Project

Step 1: For ASP.NET Core 5, in the appsettings.json file, put the connection string

"ConnectionStrings": {
    "sqlConnection": "Server=YourServerName;Database=YourDatabaseName;Integrated Security=true; Trusted_Connection=True;"
  },

(Note: If you want to use localdb, the connection string would be

"ConnectionStrings": {
    "sqlConnection": "Server=(localdb)\\mssqllocaldb;Database=YourDatabaseName;Trusted_Connection=True;"
  },

)

Where to find Server name?

  1. Open SQL Server Management Studio

  1. In the Server name field, if you do not see the server name listed, click Browse for more...

  1. Click the + sign next to Database Engine and you will see your Server name. Click on the server name and select it. Now connect to your SQL Server.

Now connect to your SQL Server.

Step 2. In the startup.cs add the service in the ConfigureServices(IServiceCollection services){ place your sql connectiono service here } method

// DatabaseContext comes from DatabaseContext.cs
            services.AddDbContext<DatabaseContext>(options => 
                options.UseSqlServer(Configuration.GetConnectionString("sqlConnection")) // sqlConnection comes from appSettings connectionString
            );

这篇关于Visual Studio 代码映射:无法连接到指定的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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