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
- Strconnection + = "Initial catalog= modified to become your own database created; data source=localhost;";
C # code
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Data.SqlClient;
- Namespace Dbapp
- {
- Class Program
- {
- static void Main (string[] args)
- {
- Console.WriteLine ("Hello Word1");
- PROGRAM.LINKDB ();
- Console.WriteLine ("Hello Word3");
- Console.ReadLine ();
- }
- static void Linkdb ()
- {
- Console.WriteLine ("Hello Word2");
- string strconnection = "UID=SA; Password=sa; ";
- Strconnection + = "initial catalog=netdb;data source=localhost;";
- Strconnection + = "Connect timeout=3";
- //String constr = "server=.; Database=myschool;integrated Security=sspi ";
- //string constr = "server=.; Database=myschool;uid=sa;pwd=sa ";
- //strconnection = "Data source=.; Initial Catalog=netdb;user Id=sa;pwd=sa ";
- Try
- {
- SqlConnection objconnection = new SqlConnection (strconnection);
- //SqlConnection objconnection = new SqlConnection (strconnection);
- Objconnection.open ();
- Console.WriteLine ("Connection to database Success");
- Objconnection.close ();
- }
- catch (Exception ex)
- {
- Console.WriteLine (ex. Message);
- }
- }
- }
- }
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