4

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
Community
  • 1
  • 1
Lord Helmet
  • 150
  • 14
  • 4
    As good as your reason for not posting code may be, it's a lot easier for you to post it than it is for us to help you without it. – Stan Shaw Jan 08 '16 at 20:12
  • Have you tested with different SQL Servers? If not, you could try create a new simple instance and connecting to that. – Greg Viers Jan 08 '16 at 20:13
  • 2
    If you don't want to post Code, please tell us at least the error message. Is it running without debugging? – etalon11 Jan 08 '16 at 20:15
  • information: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) – Lord Helmet Jan 08 '16 at 20:28
  • Have you done what the error suggests.. verify that the instance name is correct and that **SQL Server is configured to allow remote connections**? That is the most common issue I have when setting up a new data connection. – dub stylee Jan 08 '16 at 20:33
  • put your connection string inside of a .config file also look here on [how to configure connection strings in C#](http://www.connectionstrings.com) – MethodMan Jan 08 '16 at 20:35
  • Yes, and again, the connection string works in PowerShell, and visual studio can connect to the database, it's only when C# is trying to connect either in a console or Windows form. – Lord Helmet Jan 08 '16 at 20:36
  • If you want to use UserID and Password then remove Integrated Security=True – Steve Jan 08 '16 at 20:41
  • Done that too. The iteration I had posted was just when I was trying it anyway. I also tried the IP connectionString from the article that MethodMan had posted. Great attempt, to rule out any DNS settings, but I still get the same network error. If this is indeed a network error, I am not sure how I am able to authenticate in PS and VS, but not C# in VS. This is really weird...reminds me of the days of learning how to use Eclipse. – Lord Helmet Jan 08 '16 at 20:42
  • I didn't restart my workstation from when I had installed the patch mentioned in a link in the OP. Perhaps I need to try that, as I had only restarted VS. Seems to be an issue with VS 2013 and SQL 2014 that needed to be patched in VS...so I'll try restarting and see how that works. – Lord Helmet Jan 08 '16 at 20:48
  • I'll try your suggestion, @MethodMan. – Lord Helmet Jan 08 '16 at 20:56

2 Answers2

1

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

Franz Wimmer
  • 1,477
  • 3
  • 20
  • 38
  • That's a lovely little snippet you've pointed out, but it still doesn't resolve the problem. Additional information: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) – Lord Helmet Jan 11 '16 at 13:25
  • I also get this error in the output console: System.ComponentModel.Win32Exception (0x80004005): Access is denied5 The program '[9060] ConsoleApplication2.vshost.exe' has exited with code -1 (0xffffffff). – Lord Helmet Jan 11 '16 at 14:05
  • And both of these errors are interesting because (A) Visual Studio is connected to this server in both Data Connections and Servers, and (B) I have verified that I do have access with both my AD account and the service account that I want to use. – Lord Helmet Jan 11 '16 at 14:06
  • Are you sure named pipes and/or TCP/IP connections are enabled server side? – Franz Wimmer Jan 11 '16 at 14:16
  • Based on this article, all of the criteria has been met for SQL server 2014. http://support.sysonline.com/support/solutions/articles/4000016491-errmsg-a-network-related-or-instance-specific-error-occurred-while-establishing-a-connection-to-sql – Lord Helmet Jan 11 '16 at 15:04
  • This also did not work: https://gerardbeckerleg.wordpress.com/2015/07/08/visual-studio-2015-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/ – Lord Helmet Jan 11 '16 at 15:19
  • I found this was disabled, but enabling did not work either. http://stackoverflow.com/questions/21375014/i-cannot-start-sql-server-browser – Lord Helmet Jan 11 '16 at 15:33
0

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