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

Source: Internet
Author: User
Tags sql server management

Poor me ah, engaged in an afternoon, connecting C # Connection SQL server2005, is not connected,

And then check the information, check the information, not only complain about the domestic article is the same, did not explain how to write

How to configure, you reprint me, I reprint you. Of course, I also like to reprint .... , haha haha

The problem is that the connection is not SQL server2005, the error is:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. Service not found or inaccessible

Service. Verify that the instance name is correct, and that SQL Server is configured to allow remote connections. (Provider: Life

Name of the pipe provider, error:40-cannot open a connection to SQL Server)

So, look for Ah, find Ah, find Ah, found is this article of the above an article.

In fact, the connection code is almost right, but no one points out, there are port problems.

The default installed SQL Server2005 is the port is not 1433, we want 1433.

1. Open the SQL Server Management Configurator


2. Click on the SQLEXPRESS protocol and we want to start all States.


3. Select TCP/IP, right click, select Properties, we modify the port address of the connection database, it is very important


4. Modify three places, the first you see the next jump out of the dialog box, there are a lot of TCP/IP ports. We're looking for three places.


5.IP address is the address of your own computer add 1433 on the TCP port, and then select Start

6. IP address is 127.0.0.1 is the default, but also to get rid of, as above


Does the 8.IPALL represent all ports?? You have to change the address of the lower port anyway.


9. Restart the service, do not support hot modification, modify the restart service.


OK, so this is the modified port.

Paste the following code

This code can be run, so you don't have to worry, you write code is problematic, you want to modify is accustomed to your database

C # code
    1. Strconnection + = "Initial catalog= modified to become your own database created; data source=localhost;";

C # code
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Data.SqlClient;
  6. Namespace Dbapp
  7. {
  8. Class Program
  9. {
  10. static void Main (string[] args)
  11. {
  12. Console.WriteLine ("Hello Word1");
  13. PROGRAM.LINKDB ();
  14. Console.WriteLine ("Hello Word3");
  15. Console.ReadLine ();
  16. }
  17. static void Linkdb ()
  18. {
  19. Console.WriteLine ("Hello Word2");
  20. string strconnection = "UID=SA; Password=sa;  ";
  21. Strconnection + = "initial catalog=netdb;data source=localhost;";
  22. Strconnection + = "Connect timeout=3";
  23. //String constr = "server=.;  Database=myschool;integrated Security=sspi ";
  24. //string constr = "server=.;    Database=myschool;uid=sa;pwd=sa ";
  25. //strconnection = "Data source=.;  Initial Catalog=netdb;user Id=sa;pwd=sa ";
  26. Try
  27. {
  28. SqlConnection objconnection = new SqlConnection (strconnection);
  29. //SqlConnection objconnection = new SqlConnection (strconnection);
  30. Objconnection.open ();
  31. Console.WriteLine ("Connection to database Success");
  32. Objconnection.close ();
  33. }
  34. catch (Exception ex)
  35. {
  36. Console.WriteLine (ex. Message);
  37. }
  38. }
  39. }
  40. }

The effect of successful connection

--------------------------------------------------------------------------------------------------------------

If you are still not connected, then check out the following configuration:




This you are looking under.

Then restart your computer and try to connect under the code. You first have to make sure that you can go to SQL server2005 via SA

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.