简体   繁体   English

Visual Studio 2013与SQL Server 2014的连接

[英]Visual Studio 2013 Connection to SQL Server 2014

Using Visual Studio, I cannot connect to a SQL Server. 使用Visual Studio,我无法连接到SQL Server。 I think this is something specific to Visual Studio, and NOT the server itself. 我认为这是Visual Studio特有的,而不是服务器本身。 VS is on a laptop (workstation) and the server is on another subnet. VS在笔记本电脑(工作站)上,服务器在另一个子网上。 Versions are listed in the title, and I have already considered the solution below: 版本列在标题中,我已经考虑过以下解决方案:

Visual Studio 2013 incompatibility with MS SQL Server 2014 Visual Studio 2013与MS SQL Server 2014不兼容

  • My connection string works in PowerShell without issue. 我的连接字符串在PowerShell中可以正常工作。
  • My connection works in Visual Studio when connecting with the Server Explorer. 与服务器资源管理器连接时,我的连接在Visual Studio中工作。
  • 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. 连接在C#代码中不起作用,我已经把它剥离到一个基本的控制台项目,尽可能基本。
  • 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) (我现在去过30多个论坛,很容易)
  • I have tried both SQL Server authentication and Windows authentication. 我已经尝试过SQL Server身份验证和Windows身份验证。 Both forms work in PowerShell and Visual Studio Server Explorer. 这两种形式都可以在PowerShell和Visual Studio Server Explorer中使用。

Please don't mark this as irrelevant, as this is related to C# and NOT SQL. 请不要将此标记为无关紧要,因为这与C#和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. 在PowerShell,同一台机器上,此代码有效。

$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. 您应该始终使用ConnectionStringBuilder来创建连接字符串。 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; 请注意, Integrated Security=True; indicates that you want to use the current windows account credentials for authentication; 表示您要使用当前的Windows帐户凭据进行身份验证; UserID and password are unnecessary in this case. 在这种情况下,不需要用户ID和密码。

Source: https://msdn.microsoft.com/en-us/library/ms254947%28v=vs.110%29.aspx 资料来源: 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... 好吧你可以试试这个,注意当你写连接字符串时它使用两个“//”,我认为这不会有问题,这样你就可以使用appconfig或只是在类中创建字符串并声明连接字符串......

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. 下面的Stack Overflow文章解决了我的特殊解决方案。 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. 然而,当我发现SQL Server配置管理器>> SQL Server网络配置>> MSSQLSERVER协议>>命名管道被禁用时,问题立即得到解决。 I enabled this and then restarted all SQL services, and the issue resolved immediately. 我启用了此功能,然后重新启动了所有SQL服务,问题立即得到解决。 Why this is functional in PowerShell, SSMS, and Visual Studio Data Connections and Servers - but not when connecting with C# is beyond me. 为什么这在PowerShell,SSMS和Visual Studio数据连接和服务器中起作用 - 但是在与C#连接时不是我的。 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'? 如何修复错误'命名管道提供程序,错误40 - 无法打开与'SQL Server'的连接?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio 2013 中使用 C# 将文本转换为日期格式并将其保存在 SQL Server 2014 中 - Convert text into date format in Visual Studio 2013 using C# and save it in SQL Server 2014 使用SQL Server 2014 LocalDB和Visual Studio 2013的LightSwitch构建失败 - LightSwitch build fails using SQL Server 2014 LocalDB and Visual Studio 2013 如何在Visual Studio 2013中配置SQL Server以允许远程连接 - How to configure SQL Server in Visual Studio 2013 to allow remote connection Visual Studio与SQL Server的连接 - visual studio connection to sql server 无法从Visual Studio 2012连接到SQL Server Express 2014 - Cannot connect to SQL Server Express 2014 from Visual Studio 2012 SQL连接字符串和倾斜参数Visual Studio 2013 - Sql Connection string and cant parameters visual studio 2013 从Visual Studio连接到SQL Server Express - Connection to SQL Server Express from Visual Studio 使用Visual Studio 2010连接到SQL Server Express - Connection to sql server express with visual studio 2010 在Visual Studio 2013中使用SqlCeResultSet进行SQL Server Compact Edition数据访问 - SQL Server Compact Edition Data Access with the SqlCeResultSet in Visual Studio 2013 从 Visual Studio 2013 数据库项目中清理 SQL Server 数据库 - Clean SQL Server database from Visual Studio 2013 database project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM