Visual Studio Debugging Connection Database Exception resolution method

Source: Internet
Author: User
Tags stack trace connectionstrings

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or could not be accessed. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (provider: Named pipe provider, ERROR:40-Unable to open connection to SQL Server)

Description: An unhandled exception occurred during the execution of the current WEB request. Check the stack trace information For more information about the error and the source of the error in your code.

Exception Details:

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 could not be accessed. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (provider: Named pipe provider, ERROR:40-Unable to open connection to SQL Server)

When this problem occurs, it is different from the database version, the same as the ground connection method.

I used to use server= (local) or localhost;database=; User Id=sa; password=
Changed to the data source=.\\sqlexpress;initial catalog=; User Id=sa; password=

In practice, you just have to change server= (local) or localhost; for data source=.\\sqlexpress.

1. Open the Sql2008 remote connection function, open the method as follows:
Configuration Tools->sql Server Configuration Manager->sql Server network configuration->l start in two protocols, "Using TCP/IP and Named pipes" to determine that restarting the database service is OK.

The following sections are reprinted netizens, but he uses SQL2005 and VS2005, because the reason for the version is for reference only

2. Database connection string
Database connection string There are several kinds of, I believe you have seen, have been dizzy
Data source=.\sqlexpress;initial Catalog=northwind; User Id=sa; password=sa123456
Data source= server name \sqlexpress;initial catalog=northwind; User Id=sa; password=sa123456
Data source=localhost\sqlexpress;initial Catalog=northwind; User Id=sa; password=sa123456
Data Server=.;i Nitial Catalog=northwind; User Id=sa; password=sa123456
Data source= server name; Initial catalog=northwind; User Id=sa; password=sa123456
......
What kind of right? This is related to the database version, and if it is a SQL Server 2005 Express version, you must have "\sqlexpress". And if the string is defined as a variable, VS2005 will also add a red wavy line underneath "\" to suggest that "\s is an unrecognized escape sequence", so if the string is defined as a variable, it should be written server=.\\sqlexpress
5. Registering the SQL Server database
Run the "aspnet_regsql" directive under the path "C:\Windows\Microsoft.NET\Framework\v2.0.50727" and the ASP. NET SQL Server Setup Wizard Wizards appears. The SQL Server registration interface will appear after pressing the next two consecutive steps, and the database parameters you want to register are registered. After registering, you will have more tables () in your database:

6. Setting the database connection string
Open iis-> on the default Web site or the virtual directory where the Web site is located, right-click Properties, select the ASP. Edit configuration, and on the General tab edit the LocalSqlServer database connection string:
Data server=.\sqlexpress;initial Catalog=northwind; User Id=sa; password=sa123456
7. Setting up 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>
That's it. below to test
1. Easy Connection Test
Loose connection test using SqlDataSource
VS2005 Server Explorer Select database Right-click, select Modify Connection, fill in the server name, fill in the user name with SQL Server Authentication sa and password sa123456-> Select or enter a database name: northwind- > Click Test Connection
Loose connection Test Connection success does not indicate that the database is connected OK, at first I was hanging here, loose connection test test connection is successful, but always can not read out the contents of the database into the page.
2. Rigorous connection Testing
Select Site Menu->asp in VS2005. NET configuration into the ASP. Select Provider, click AspNetSqlProvider Test, if the test is successful, the database settings are correct, otherwise you have to start over.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.