Connect Visual Studio 2013 to SQL Server 2014

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
+4
source share
3 answers

" " . , , , , , , , , . , , , SQL Server → SQL Server → MSSQLSERVER → . , SQL, . PowerShell, SSMS Visual Studio Data Connections Servers, # . , , , , . , , , ( -).

'Named Pipes Provider, 40 - SQL Server?

-1

ConnectionStringBuilder . :

System.Data.SqlClient.SqlConnectionStringBuilder builder =
    new System.Data.SqlClient.SqlConnectionStringBuilder();
builder.DataSource = "SERVERNAME";
builder.IntegratedSecurity = true;
builder.InitialCatalog = "DATABASE";
builder.UserID = "userid";
builder.Password = "password";
Console.WriteLine(builder.ConnectionString);

:

Data Source=SERVERNAME;Initial Catalog=DATABASE;Integrated Security=True;User ID=userid;Password=password

, Integrated Security=True; , Windows ; .

: https://msdn.microsoft.com/en-us/library/ms254947%28v=vs.110%29.aspx

+1

Well, you try this, note that when you write a connection string, it uses two "//", and I think that it will not be a problem, and so you can use appconfig or just create a line in the class and declare a line conveyor belt ...

First create a class:

    public static class Connetion_string
    {
      public static string conection = "Server=.\\Server_name;Initial Catalog=Data_base_name;Integrated Security=True";
    }

Then you could write something like this ...

     public void Some_procedure()
     {
        SqlConnection con = new SqlConnection(Conection_string.conection);
        try
            {
                con.Open();
                //Here the code to execute soomething from data base....
                con.Close();
            }
            catch (Exception ex)
            {
                string ms;
                ms = ex.Message;
            }
            //This will ensure that al resources in server are available
            finally
            {
                con.Close();
            }
    }
0
source

Source: https://habr.com/ru/post/1623476/


All Articles