简体   繁体   English

通过控制台应用程序连接到 SQL Server 2014 不起作用

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

相关问题 在安装exe应用程序中无法连接到本地SQL Server数据库 - Connecting to a local SQL Server database does not work in the setup exe application 在Windows Server 2012上将IIS连接到SQL Server 2014 - Connecting IIS to SQL Server 2014 on Windows Server 2012 EF 6是否支持SQL Server 2014类型? - Does EF 6 support SQL Server 2014 Types? C#代码与SQL Server 2014 Express的连接不起作用,无法打开连接 - Connection from C # code with SQL Server 2014 Express does not work , can not open the connection 奇怪的缓存问题EF代码优先应用程序SQL Server 2014 - Weird Caching Issue EF Code First Application SQL Server 2014 SQL Server 2014中的NullReferenceException - NullReferenceException in SQL Server 2014 通过TCP / IP连接到SQL服务器,而不是NP - Connecting to SQL server via TCP/IP, not NP 通过 EF Core 连接到 ASP.NET Core 应用程序中的 SQL 服务器时,pipe 的另一端没有进程 - No process is on the other end of pipe while connecting to SQL Server in an ASP.NET Core application via EF Core C#-C#服务器用作控制台应用程序。 不能用作Web应用程序 - C# - C# Server works as Console Application. Does not work as Web Application 连接SQL Server的程序中的应用程序角色 - Application Role in Program connecting the SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM