[英]Connecting to SQL Server 2014 via console application does not work
I'm trying to read out values from my database.我正在尝试从我的数据库中读出值。 I'm pretty sure that the method I use is correct.
我很确定我使用的方法是正确的。 Also my SQL Server 2014 allows remote access.
我的 SQL Server 2014 也允许远程访问。
Nevertheless when I run my application I get an error.然而,当我运行我的应用程序时,我收到一个错误。
System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server.
System.Data.SqlClient.SqlException:建立与 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.
验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (provider: SQL Network Interfaces, error: 25 - Connection string is not valid)
(提供程序:SQL 网络接口,错误:25 - 连接字符串无效)
My code:我的代码:
private void DatabaseUpgradeCheckForMSSQL()
{
SqlConnection sqlCon = new SqlConnection("Data Source=VBKSQL2014MSSQL\\MSSQLSERVER; Initial Catalog=dwworkflowengine; User ID=sa; Password=<Password>;");
SqlDataAdapter sqlda = new SqlDataAdapter("Select * from Changesets", sqlCon);
DataTable dtbl = new DataTable();
sqlda.Fill(dtbl);
foreach (DataRow row in dtbl.Rows)
{
Console.WriteLine(row["Number"]);
}
Console.ReadKey();
}
If you're connecting to the default, unnamed instance of SQL Server, which does have the internal "ID" of MSSQLSERVER
, you must not use that internal ID as the instance name.如果您连接到默认的、未命名的 SQL Server 实例,该实例的内部“ID”为
MSSQLSERVER
,则不得使用该内部 ID 作为实例名称。
So instead of this:所以而不是这个:
SqlConnection sqlCon = new SqlConnection("Data Source=VBKSQL2014MSSQL\\MSSQLSERVER; Initial Catalog=dwworkflowengine; User ID=sa; Password=<Password>;");
try this:尝试这个:
SqlConnection sqlCon = new SqlConnection("Data Source=VBKSQL2014MSSQL;Initial Catalog=dwworkflowengine;User ID=sa;Password=<Password>;");
And please - as a Best Practice - do NOT use the sa
account for your work!!并请-作为一个最佳实践-不使用
sa
帐户为您的工作! Use integrated security, or a separate account - sa
should NEVER be used in your real life work!使用集成安全性或单独的帐户 -
sa
永远不应在您的现实工作中使用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.