如何修复错误“命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接" [英] How to fix error "Named Pipes Provider, error: 40 - Could not open a connection to SQL Server"

查看:41
本文介绍了如何修复错误“命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在尝试制作一个简单的 C# 控制台应用程序,它只查询数据库——仅此而已.

So I'm trying to make a simple C# console app that simply queries a database - nothing more.

但是,我不断收到此错误:

However, I keep getting this error:

未处理的异常类型'System.Data.SqlClient.SqlException'发生在 System.Data.dll

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

附加信息:A网络相关或特定于实例建立时发生错误连接到 SQL Server.服务器未找到或无法访问.验证实例名称是否为正确,并且 SQL Server 是配置为允许远程连接.(提供者:命名管道提供程序,错误:40 - 无法打开连接到 SQL Server)

Additional information: 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)

现在,我什么都试过了!我已经编辑了我的所有设置(添加了防火墙例外,启用了 TCP/IP,几乎可以通过谷歌搜索找到任何解决方案,我做到了).当我尝试使用 SQL Management Studio 使用连接字符串中的相同凭据建立连接时,一切正常.但是由于某种原因,Visual Studio 没有任何效果.

Now, I've tried EVERYTHING! I've edited all my settings(added firewall exception, enabled TCP/IP, just about any solution you can find with a google search, I did). When I try to make a connection using SQL Management Studio using the same credentials that are in my connection string, everything works PERFECTLY. BUt for some reason, nothing works from Visual Studio.

这是我的连接字符串:

<add name="Invoicing"
            connectionString="Data Source=server;Initial Catalog=Invoicing;Persist Security Info=True;ID=id;Password=pw"
            providerName="System.Data.SqlClient" />

这是应用程序:

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

        Invoicing db = new Invoicing("Invoicing");

        var q = from sin Invoice
                where s.Date == 201007 
                select s;

        foreach(var sin q)
            Console.WriteLine("{0}, {1}", s.CreateDate,
                              s.EndDate);


        }
    }

这是我的堆栈跟踪:

   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Linq.SqlClient.SqlConnectionManager.UseConnection(IConnectionUser user)
   at System.Data.Linq.SqlClient.SqlProvider.get_IsSqlCe()
   at System.Data.Linq.SqlClient.SqlProvider.InitializeProviderMode()
   at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.GetQueryText(Expression query)
   at System.Data.Linq.DataQuery`1.ToString()

如果重要的话,我使用 SqlMetal 创建了 DataContext.

I created the DataContext using SqlMetal, if that matters.

另外,我使用的是 .Net 2008 和 SQL 2008.

Also, I am using .Net 2008 and SQL 2008.

有人知道该怎么做吗?

推荐答案

以下是否有效可能值得测试:

It's probably worth just testing if the following works:

SqlConnection con = new SqlConnection("Data Source=server;Initial Catalog=Invoicing;Persist Security Info=True;ID=id;Password=pw");

在我看来,您的连接字符串可能不正确 - 没有提供用户.我使用以下连接字符串:数据源=ServerNameHere;初始目录=DBNameHere;用户ID=XXXX;密码=XXXX"

To me it looks like your connection string is probably not right - there's no user provided. I use the following connection string: "Data Source=ServerNameHere;Initial Catalog=DBNameHere;User ID=XXXX;Password=XXXX"

这篇关于如何修复错误“命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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