Using Visual Studio, I cannot connect to SQL Server. I think this is something special for Visual Studio, and NOT for the server itself. VS is on the laptop (workstation), and the server is on a different subnet. The versions are indicated in the name, and I have already considered the solution below:
Incompatibility of Visual Studio 2013 with MS SQL Server 2014
- The connection string works in PowerShell without any problems.
- My connection works in Visual Studio when connected to Server Explorer.
- The connection does not work in C # code, I got to the point that he deleted it in the base console project in order to become as basic as possible.
- I tried all iterations of the connection string that I could find on the forums. (I've already over 30 forums, easy)
- I tried SQL Server Authentication and Windows Authentication. Both forms work in PowerShell and Visual Studio Server Explorer.
Do not put this as irrelevant, as it is related to C # and NOT SQL. The server is configured 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, the 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
source
share