Network-related or instance-specific errors occur when you establish a connection with SQL Server. The server is not found or cannot be accessed. Verify that the Instance name is correct and SQL Server is configured to allow remote connection. (Provider: named pipeline provider, error: 40-unable to open the connection to SQL Server)
Note: An unhandled exception occurs during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.
Exception details: System. Data. SqlClient. SqlException: Network-related or instance-specific errors occur when you establish a connection with SQL Server. The server is not found or cannot be accessed. Verify that the Instance name is correct and SQL Server is configured to allow remote connection. (Provider: named pipeline provider, error: 40-unable to open the connection to SQL Server)
When this problem occurs, it is actually very simple, it is different database versions, the method of connecting to the ground is the same; pay attention to the following red part. I used to use Server =.; database = jtj; User ID = sa; Password =
Changed to Server =. SQLEXPRESS; Initial Catalog = jtj; User ID = sa; Password =
It took a long time to get it done.
1. Enable the sql2005 remote connection function. The method is as follows:
Configuration tool-> SQL Server peripheral application configurator-> service and connected peripheral application configurator-> open the Database Engine node under the MSSQLSERVER node, select "remote connection" first ", next, we recommend that you select "use TCP/IP and named pipes at the same time". Then, restart the database service.
2. Change the logon settings to the SQL Server and Windows authentication modes. The specific settings are as follows:
SQL Server Management Studio manager-> Windows Authentication connection Server-> choose your data Server in object Resource Manager-> right-click-> properties-> Security-> SQL Server and Windows Authentication select the mode.
3. Set a user name and password for SQL Server as follows:
(1) SQL Server Management Studio manager-> Windows Authentication connection Server-> select your data Server in object Resource Manager-> Expand "security" on the Server-> login name-> On the sa right-click an account and choose "select"> "general"> "Change sa Logon account password. In this way, a user with the username sa and password sa123456 is set.
(2) Select status on the "Select page"-> change logon to Enabled
4. database connection string
There are several database connection strings. I believe you have seen them before, and they are all lost.
Data Server =. SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sa123456
Data Server = Server name SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sa123456
Data Server = localhostSQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sa123456
Data Server =.; Initial Catalog = Northwind; User ID = sa; Password = sa123456
Data Server = Server name; Initial Catalog = Northwind; User ID = sa; Password = sa123456
......
Which is true? This is related to the database version. for SQL Server 2005 Express, "SQLEXPRESS" is required ". Moreover, if this string is defined as a variable, VS2005 will add a red wavy line under "" to prompt you that "S is an unidentifiable escape sequence ", therefore, if the string is defined as a variable, it should be written as Server =. SQLEXPRESS
5. register an SQL Server database
In the path "C: WindowsMicrosoft. ASP appears when you run the "ASPNET_REGSQL" command under NETFrameworkv2.0.50727. net SQL Server Setup Wizard, after two consecutive steps are followed, the SQL Server registration page appears. Enter the database parameters you want to register and register. After registration, several more tables () will be added to your database ():
5. Set the database connection string
Open IIS-> right-click the default website or the virtual directory of the website and choose Properties-> select ASP. NET tab-> edit configuration-> on the "General" tab, edit the "LocalSqlServer" database connection string:
Data Server =. SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sa123456
6. Set the web. config file
Add the following program to the web. config file:
<ConnectionStrings>
<Add name = "LocalSqlServer" connectionString = "Data Source =. SQLEXPRESS; Initial Catalog = Northwind; User ID = sa; Password = sa123456 "providerName =" System. data. sqlClient "/>
</ConnectionStrings>
In this way, we are done. Next we will test
1. Loose connection test
Use SqlDataSource for loose connection Testing
In VS2005 Server resource manager, right-click the database and choose modify connection> enter Server Name> use SQL Server authentication to enter the user name sa and password sa123456-> select or enter a Database Name: northwind-> click test connection
The connection to the loose connection test is successful, but the connection to the database is OK. At the beginning, I am here. The Loose connection test is successful, however, the database content cannot be read to the webpage.
2. Rigorous connection tests
In VS2005, choose website menu> ASP. NET configuration to enter ASP. NET website management tool-> select the provider-> click the test of AspNetSqlProvider. If the test is successful, the database settings are correct. Otherwise, you have to check the problem from the beginning.