Trouble connecting to local SQL Server database

时间:2019-01-15 18:13:53

标签: c# sql-server visual-studio ssms sql-server-express

Just trying to do a simple local database connect to Microsoft SQL Server in Visual Studio. Trying to figure out why it's not connecting. I made a user specifically for this and have the correct username and password. I omitted the User Id and Password from this purposely. Seems to be throwing an error at my connection string, what am I doing wrong?

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)

My code:

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; 
        }
    }
}

2 个答案:

答案 0 :(得分:0)

如果您使用的是SQL Server Express ,并且在安装时使用了所有默认设置,则您有一个名为SQLEXPRESS的SQL Server 命名实例。 -因此,您需要使用以下连接字符串:

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

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

答案 1 :(得分:-1)

整天进行故障排除后,本指南对您有所帮助。

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

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

我要感谢所有不支持投票的人,并为我提供了帮助,对此我感到非常感谢。我将Marc_S标记为答案,但想为其他可能会读到此内容的人添加此内容。