Named Pipes Provider, error: 40 - Could not open a

如果用SQLServer的SQLEXPRESS版本的話,連接時經常回遇到這個問題,查了很多終於找到了解決方法。

The typical error when dealing with Express includes:

a. User is not aware of SqlExpress was installed as a named instance, consequently, in his/her connection string, he/she only specify ".","localhost" etc instead of ".\SqlExpress" or "\Sqlexpress".

b. Np was disabld by default after installing SqlExpress.

c. If Sqlexpress was installed on the remote box, you need to enable remote connection for Express.

其中a的.\SqlExpress必須寫在web.config文件中,如:

<connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=.\SqlExpress;Initial Catalog=Northwind;User ID=sa;Password=test"
   providerName="System.Data.SqlClient" />
</connectionStrings>

再用ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString把connectionStrings從應用程序中讀出來。如果直接把.\SqlExpress寫在應用程序中如:SqlConnection conn = new SqlConnection("data source=.\ SqlExpress;initial catalog=Northwind;user id=sa;password=test")這樣也是有錯誤的!

Please read the following blog for best practice of connecting to SqlExpress.

http://blogs.msdn.com/sql_protocols/archive/2006/03/23/558651.aspx

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章