2010-10-01 99 views
6

所以我試圖做一個簡單的查詢數據庫的簡單的C#控制檯應用程序 - 僅此而已。如何修復錯誤「命名管道提供程序,錯誤:40 - 無法打開連接到SQL Server」

不過,我不斷收到此錯誤:

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

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沒有任何工作。

這裏是我的連接字符串:

<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的,如果事項。

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

有沒有人有什麼做什麼想法?

回答

3

這可能是值得只是測試,如果以下工作:

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

對我來說,它看起來像您的連接字符串可能不正確 - 沒有用戶提供。 我用下面的連接字符串: 「數據源= ServerNameHere;初始目錄= DBNameHere;用戶ID = XXXX;密碼= XXXX」

+0

哇,我用實際的字符串替換了對連接字符串的引用,現在它可以工作了!謝謝!!!你知道這會發生什麼嗎?此外,我確實需要使用用戶ID,而不僅僅是字符串中的ID – James 2010-10-01 16:48:43

2

我不認爲「身份證」是正確的。我知道有許多別名,這些東西在連接字符串,但我總是用「用戶ID」

看一看here

+0

是的,我沒有注意到!謝謝! +1 – James 2010-10-01 16:49:13

3

你說你啓用TCP/IP,但是你禁用命名管道?我已經遇到過這種情況,並禁用命名管道使用Sql Server Configuration Manager爲我解決它。

你也可以在連接字符串specify the protocol,所以你可能也想試試。

+0

是的,我甚至沒有想過這樣做。不過,我已經啓用了。投票,因爲這仍然是一個有用的提示! – James 2010-10-01 16:50:00

相關問題