简体   繁体   中英

Visual Studio 2013 Connection to SQL Server 2014

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

You should always use a ConnectionStringBuilder to create your connection strings. This ensures a valid syntax and avoids malicious injections:

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);

For your Example, this produces the following output:

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

Please note that Integrated Security=True; indicates that you want to use the current windows account credentials for authentication; UserID and password are unnecessary in this case.

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

Ok you coud try this, note that when you are writing the connection string it uses two "//", and I think it would not be a problem, and in this way you coud use the appconfig or just create the string in a class and declare the conection string...

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();
            }
    }

The below Stack Overflow article addresses MY PARTICULAR SOLUTION. I am sure there is a multitude of other reasons others may have, and perhaps the steps I tried and posted in this thread may have led up to this final moment. Nevertheless, the issue was immediately resolved when i found that SQL Server Configuration Manager >> SQL Server Network Configuration >> Protocols for MSSQLSERVER >> Named Pipes was disabled. I enabled this and then restarted all SQL services, and the issue resolved immediately. Why this is functional in PowerShell, SSMS, and Visual Studio Data Connections and Servers - but not when connecting with C# is beyond me. I guess it's just one of those mysteries, but at least it's resolved. I hope this article provides a good collection of useful steps to troubleshoot this issue, thanks to everyone for helping (implicitly and otherwise).

How do I fix the error 'Named Pipes Provider, error 40 - Could not open a connection to' SQL Server'?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM