[英]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不兼容
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.