Reputation: 1460
I have a SQL Server 2008 installation on my local system, and the following code in C#, in .NET 4.5.
private Boolean ConnectDatabase()
{
if( !this.IsInitialized())
{
return false;
}
else
{
try
{
String ConnectionString = "User ID=" + this.DatabaseUsername + ";" +
"Password=" + this.DatabasePassword + ";" +
"Server=" + this.DatabaseHost + "," + this.DatabasePort + ";" +
"Database=" + this.DatabaseResource + ";";
this.DatabaseConnection = new SqlConnection(ConnectionString);
this.DatabaseConnection.Open();
return true;
}
catch (Exception e)
{
return false;
}
}
}
I can connect to the server in SSMS, but using the same credentials I am unable to get a connection in C#.
Connection string is as follows:
User ID=sa;Password=******;Server=CPUNAME\MSSQL,1433;Database=DBNAME;
Where CPUName
, DBName
, and ** are replaced with their appropriate values. I've also tried using Windows Credentials, and a variety of different server endpoints, including my NB Name, Loopback IP, Static internal IP, blah blah blah. Firewall is OFF. TCP is enabled on the server.
I get the following exception:
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: TCP Provider, error: 0 - No connection could be made because the target machine actively refused it.)
Any suggestions?
Upvotes: 0
Views: 1359
Reputation: 7140
There are a number of services that have to be running in order to connect, even locally. Some of these (helpfully) are set to "manual start" by default. Services you might want to check include the SQL Server, SQL Server Agent and the Distributed Transaction Coordinator.
Upvotes: 1
Reputation: 8821
Did you enable a protocol in the SQL Server Configuration Manager?
See Enable TCP/IP Network Protocol for SQL Server
[Edit]
Nevermind; I missed the "TCP is enabled on the server" part ;-)
The question then becomes: Did you restart SQL server after enabling TCP/IP? You need to restart the SQL service (see step 6) for these changes to become effective.
Also verify you're using the correct instancename (You're currently using MSSQL
as instancename, this might be SQLExpress
or something).
Upvotes: 1