无法连接到本地 SQL 服务器数据库

Trouble connecting to local SQL Server database

只是想做一个简单的本地数据库连接到 Visual Studio 中的 Microsoft SQL 服务器。试图弄清楚为什么它没有连接。我专门为此创建了一个用户,并拥有正确的用户名和密码。我故意省略了用户名和密码。我的连接字符串似乎出错了,我做错了什么?

System.Data.SqlClient.SqlException: 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)

我的代码:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Assetmvc
{
    public partial class SearchPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // Connect to the db
            string connStr = "Server=localhost;Database=AssetTracking;User Id=;Password=; ";
            SqlConnection conn = new SqlConnection(connStr);
            conn.Open();

            // Create a command
            SqlCommand cmd = new SqlCommand("SELECT [EmployeeId],[FirstName],[LastName],[HiredDate],[FiredDate],[CurrentItems],[SupervisorId],[SupervisorName] From [dbo].Employees");
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = conn;

            string temp = "";

            // Read from database
            SqlDataReader reader = cmd.ExecuteReader();

            while(reader.Read())
            {
                temp += reader["EmployeeId"].ToString();
                temp += reader["FirstName"].ToString();
                temp += reader["LastName"].ToString();
                temp += reader["HiredDate"].ToString();
                temp += reader["FiredDate"].ToString();
                temp += reader["CurrentItems"].ToString();
                temp += reader["SupervisorId"].ToString();
                temp += reader["SupervisorName"].ToString();
                temp += 

                temp += "<br/>";
            }

            conn.Close();

            lbl_test.Text = temp; 
        }
    }
}

如果您正在使用 SQL 服务器 Express 并且在安装时使用了所有默认设置,那么您有一个 SQL 服务器 名为 SQLEXPRESS 的命名实例 - 因此,您需要使用此连接字符串:

string connStr = "Server=localhost\SQLEXPRESS;Database=AssetTracking;User Id=;Password=; ";

请注意,您需要使用 localhost\SQLEXPRESS 连接到实例名称为 SQLEXRPESS.

命名实例

经过一整天的故障排除后,本指南帮了大忙。

https://success.scribesoft.com/s/article/Named-Pipes-Provider-Error-40

尽管这样做之后,在 Sql 服务器资源管理器中单击我的服务器后,我最终只使用了属性框中提供的连接字符串。

我要感谢所有没有投票并实际提供帮助的人,我真的很感激。我将 Marc_S 标记为答案,但想为可能阅读此内容的任何其他人添加此内容。