Click here to Skip to main content
15,943,406 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi. I have copied my project on another PC. Project includes working with database that is on Sql Server. The app is working perfectly when my PC is turned on, but when I turn off my PC the app on another PC is not working (it comes up with an error). This is the error:

C#
A network-related or instance-specific error occurred while establishing a connection to SQL Server.

The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.

(provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 1326)


EDIT:This is my connection string:
C#
Data Source=JOHN-PC\SQLEXPRESS;Initial Catalog=MyDatabase;Persist Security Info=True;User ID=MyUsername;Password=MyPassword


If I change Data Source to .\SQLEXPRESS the error comes up:

C#
Login failed for MyUsername user.


What I have tried:

Since I'm new in working with SQL Server, recently I found out that there is an option in new SQL Server versions that is called AlwaysON. Is there any connection between this option and my problem? And how can I deal with my problem?
Posted
Updated 29-Feb-16 1:01am
v2
Comments
Jörgen Andersson 29-Feb-16 5:12am    
Have a look at the connection string, I guess it's pointing to your pc and not a local file.
PeMaCN 29-Feb-16 5:48am    
Yes, it's showing to my pc but what should I type to point it to local file
Jörgen Andersson 29-Feb-16 7:03am    
Go to https://www.connectionstrings.com/sql-server-2008/ and look for "Attach a database file, located in the data directory, on connect to a local SQL Server Express instance"
PeMaCN 29-Feb-16 7:08am    
I dont think attaching database is a good idea
Jörgen Andersson 29-Feb-16 7:15am    
Why not?

1 solution

Hi,

This issue may be because of SQL Server "Services".

The Sql Server service mode may be - "Manual". You need to make that "Automatic".

Steps:

Run -> type - services.msc

Find - SQL Server - Instance and change the Startup Type from Manual to Automatic

Thanks,
Parveen Siwach
 
Share this answer
 
Comments
PeMaCN 29-Feb-16 5:46am    
No.It's already automatic
Parveen Siwach 29-Feb-16 6:04am    
And what is the status --- Is it "Running"?
Parveen Siwach 29-Feb-16 6:36am    
Also you can try these:



1. Make sure your database engine is configured to accept remote connections:
Start > All Programs > SQL Server 2005 > Configuration Tools > SQL Server Surface Area Configuration
Click on Surface Area Configuration for Services and Connections
Select the instance that is having a problem > Database Engine > Remote Connections
Enable local and remote connections
Restart instance

2. You may need to create an exception on the firewall for the SQL Server instance and port you are using:
Start > Run > Firewall.cpl
Click on exceptions tab
Add sqlservr.exe (typically located in C:\Program Files (x86)\Microsoft SQL Server\MSSQL.x\MSSQL\Bin, check your installs for the actual folder path) and port (default is 1433)
Check your connection string as well

From FIX : ERROR : Could not open a connection to SQL Server:

3. Check if your SQL server services is up and running properly:
Go to All Programs > Microsoft SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager > SQL Server Services
Check to make sure SQL Server service status is Running.

In addition, ensure that your remote server is in the same network. Run sqlcmd -L to ascertain if your server is included in your network list.

4. Enable TCP/IP in SQL Server Configuration

When two or more SQL Servers are connected across network they do all communication using TCP/IP. The default port of SQL Server installation is 1433. This port can be changed through SQL Server Configuration Manager. TCP/IP should be enabled for SQL Server to be connected.
Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> Select TCP/IP
Right Click on TCP/IP >> Click on Enable

You must restart SQL Server Services for all the changes to take effect. Right click and go to menu properties to select location where default port of SQL Server can be changed.

PeMaCN 29-Feb-16 6:50am    
I have done all of these and the status is running

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900