Using Visual Studio, I cannot connect to a SQL Server. I think this is something specific to Visual Studio, and NOT the server itself. VS is on a laptop (workstation) and the server is on another subnet. Versions are listed in the title, and I have already considered the solution below:
Visual Studio 2013 incompatibility with MS SQL Server 2014
- My connection string works in PowerShell without issue.
- My connection works in Visual Studio when connecting with the Server Explorer.
- Connection does not work in the C# code, I've gone so far as stripping it down to a basic console project, to become as basic as possible.
- I have tried all iterations of the connection string that I could possibly find in forums. (I've been to over 30 forums by now, easily)
- I have tried both SQL Server authentication and Windows authentication. Both forms work in PowerShell and Visual Studio Server Explorer.
Please don't mark this as irrelevant, as this is related to C# and NOT SQL. The server is set up correctly for remote access and connection types.
using System;
using System.Data.SqlClient;
namespace ConsoleApplication2 {
class Program {
static void Main(string[] args) {
System.Data.SqlClient.SqlConnectionStringBuilder builder = new System.Data.SqlClient.SqlConnectionStringBuilder();
builder.DataSource = "SERVERNAME";
builder.InitialCatalog = "DATABASE";
builder.IntegratedSecurity = true;
Console.WriteLine(builder.ConnectionString);
SqlConnection conn = new SqlConnection(builder.ConnectionString);
conn.Open();
Console.WriteLine(conn.State);
}
}
}
In PowerShell, same machine, this code works.
$dataSource = "SERVERNAME"
$database = "DATABASE"
$connectionString = "Server = $dataSource; Database = $database; Integrated Security = $TRUE;"
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString
$connection.Open()
Write-Host $connection.State