SQL连接字符串的连接错误

时间:2019-08-01 20:30:58

标签: sql sql-server

我正在关注一个白皮书,该白皮书是一个简单的VS应用程序,它使用.NET连接到SQL Server 2012数据库。

https://www.mssqltips.com/sqlservertip/5677/how-to-get-started-with-sql-server-and-net/

我的应用程序所在的主机是Windows 7计算机。 我有一个包含Windows NT 6.2上的SQL Server 2012企业版安装的vmware虚拟服务器。该虚拟服务器的主机名是TESTBOX。 SQL Server实例的名称为SQL12_TEST1。 我可以从Windows机器从SSMS客户端连接到数据库,但是运行该应用程序时出现异常。

这是我在VS项目中的代码:

using System.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestSQLApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string connString = @"Server =TESTBOX\SQL12_TEST1; Database  = master; Trusted_Connection = True;";
            try
            {
                using (SqlConnection conn = new SqlConnection(connString))
                {
                    string query = @"SELECT @@VERSION";
                    SqlCommand cmd = new SqlCommand(query, conn);
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            Console.WriteLine(dr.GetString(0));
                        }

                    }
                    else
                    {
                        Console.WriteLine("No data found.");
                    }
                    dr.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
            }
        }
    }
}

项目编译。运行时,出现异常:抛出异常:System.Data.dll中的'System.Data.SqlClient.SqlException'

这是SQL Server日志文件:

2019-08-01 15:56:06.91 Server      Microsoft SQL Server 2012 - 11.0.2100.60 (X64) 
    Feb 10 2012 19:39:15 
    Copyright (c) Microsoft Corporation
    Enterprise Evaluation Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)

2019-08-01 15:56:06.92 Server      (c) Microsoft Corporation.
2019-08-01 15:56:06.92 Server      All rights reserved.
2019-08-01 15:56:06.92 Server      Server process ID is 1364.
2019-08-01 15:56:06.93 Server      System Manufacturer: 'VMware, Inc.', System Model: 'VMware Virtual Platform'.
2019-08-01 15:56:06.93 Server      Authentication mode is MIXED.
2019-08-01 15:56:06.93 Server      Logging SQL Server messages in file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL12_TEST1\MSSQL\Log\ERRORLOG'.
2019-08-01 15:56:06.93 Server      The service account is 'NT Service\MSSQL$SQL12_TEST1'. This is an informational message; no user action is required.
2019-08-01 15:56:06.94 Server      Registry startup parameters: 
     -d C:\Program Files\Microsoft SQL Server\MSSQL11.SQL12_TEST1\MSSQL\DATA\master.mdf
     -e C:\Program Files\Microsoft SQL Server\MSSQL11.SQL12_TEST1\MSSQL\Log\ERRORLOG
     -l C:\Program Files\Microsoft SQL Server\MSSQL11.SQL12_TEST1\MSSQL\DATA\mastlog.ldf
2019-08-01 15:56:06.94 Server      Command Line Startup Parameters:
     -s "SQL12_TEST1"
2019-08-01 15:56:07.43 Server      SQL Server detected 2 sockets with 1 cores per socket and 1 logical processors per socket, 2 total logical processors; using 2 logical processors based on SQL Server licensing. This is an informational message; no user action is required.
2019-08-01 15:56:07.43 Server      SQL Server is starting at normal priority base (=7). This is an informational message only. No user action is required.
2019-08-01 15:56:07.43 Server      Detected 2047 MB of RAM. This is an informational message; no user action is required.
2019-08-01 15:56:07.43 Server      Using conventional memory in the memory manager.
2019-08-01 15:56:09.61 Server      This instance of SQL Server last reported using a process ID of 1368 at 8/1/2019 3:55:37 PM (local) 8/1/2019 7:55:37 PM (UTC). This is an informational message only; no user action is required.
2019-08-01 15:56:09.62 Server      Node configuration: node 0: CPU mask: 0x0000000000000003:0 Active CPU mask: 0x0000000000000003:0. This message provides a description of the NUMA configuration for this computer. This is an informational message only. No user action is required.
2019-08-01 15:56:09.63 Server      Using dynamic lock allocation.  Initial allocation of 2500 Lock blocks and 5000 Lock Owner blocks per node.  This is an informational message only.  No user action is required.
2019-08-01 15:56:09.65 Server      Software Usage Metrics is disabled.
2019-08-01 15:56:09.70 spid8s      Starting up database 'master'.
2019-08-01 15:56:10.04 Server      CLR version v4.0.30319 loaded.
2019-08-01 15:56:10.41 spid8s      Resource governor reconfiguration succeeded.
2019-08-01 15:56:10.42 spid8s      SQL Server Audit is starting the audits. This is an informational message. No user action is required.
2019-08-01 15:56:10.43 spid8s      SQL Server Audit has started the audits. This is an informational message. No user action is required.
2019-08-01 15:56:10.99 spid8s      SQL Trace ID 1 was started by login "sa".
2019-08-01 15:56:10.99 Server      Common language runtime (CLR) functionality initialized using CLR version v4.0.30319 from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\.
2019-08-01 15:56:11.09 spid8s      Server name is 'TESTBOX\SQL12_TEST1'. This is an informational message only. No user action is required.
2019-08-01 15:56:11.27 spid13s     A self-generated certificate was successfully loaded for encryption.
2019-08-01 15:56:11.49 spid13s     Server is listening on [ 'any' <ipv6> 1433].
2019-08-01 15:56:11.52 spid13s     Server is listening on [ 'any' <ipv4> 1433].
2019-08-01 15:56:11.54 spid13s     Server local connection provider is ready to accept connection on [ \\.\pipe\SQLLocal\SQL12_TEST1 ].
2019-08-01 15:56:11.55 spid13s     Server local connection provider is ready to accept connection on [ \\.\pipe\MSSQL$SQL12_TEST1\sql\query ].
2019-08-01 15:56:11.58 Server      Server is listening on [ ::1 <ipv6> 53312].
2019-08-01 15:56:11.58 Server      Server is listening on [ 127.0.0.1 <ipv4> 53312].
2019-08-01 15:56:11.58 Server      Dedicated admin connection support was established for listening locally on port 53312.
2019-08-01 15:56:11.63 spid13s     SQL Server is now ready for client connections. This is an informational message; no user action is required.
2019-08-01 15:56:11.63 Server      SQL Server is attempting to register a Service Principal Name (SPN) for the SQL Server service. Kerberos authentication will not be possible until a SPN is registered for the SQL Server service. This is an informational message. No user action is required.
2019-08-01 15:56:11.64 Server      The SQL Server Network Interface library could not register the Service Principal Name (SPN) [ MSSQLSvc/TestBox:SQL12_TEST1 ] for the SQL Server service. Windows return code: 0xffffffff, state: 63. Failure to register a SPN might cause integrated authentication to use NTLM instead of Kerberos. This is an informational message. Further action is only required if Kerberos authentication is required by authentication policies and if the SPN has not been manually registered.
2019-08-01 15:56:11.64 Server      The SQL Server Network Interface library could not register the Service Principal Name (SPN) [ MSSQLSvc/TestBox:1433 ] for the SQL Server service. Windows return code: 0xffffffff, state: 63. Failure to register a SPN might cause integrated authentication to use NTLM instead of Kerberos. This is an informational message. Further action is only required if Kerberos authentication is required by authentication policies and if the SPN has not been manually registered.
2019-08-01 15:56:25.56 spid14s     A new instance of the full-text filter daemon host process has been successfully started.
2019-08-01 15:56:25.71 spid9s      Starting up database 'mssqlsystemresource'.
2019-08-01 15:56:25.71 spid17s     Starting up database 'msdb'.
2019-08-01 15:56:25.71 spid18s     Starting up database 'AdventureWorks'.
2019-08-01 15:56:25.72 spid9s      The resource database build version is 11.00.2100. This is an informational message only. No user action is required.
2019-08-01 15:56:25.75 spid19s     Starting up database 'TSQL2012'.
2019-08-01 15:56:25.80 spid20s     Starting up database 'AdventureWorksDW'.
2019-08-01 15:56:25.90 spid9s      Starting up database 'model'.
2019-08-01 15:56:26.13 spid9s      Clearing tempdb database.
2019-08-01 15:56:26.64 spid9s      Starting up database 'tempdb'.
2019-08-01 15:56:26.98 spid21s     The Service Broker endpoint is in disabled or stopped state.
2019-08-01 15:56:27.03 spid21s     The Database Mirroring endpoint is in disabled or stopped state.
2019-08-01 15:56:27.27 spid21s     Service Broker manager has started.
2019-08-01 15:56:27.54 spid8s      Recovery is complete. This is an informational message only. No user action is required.
2019-08-01 15:56:30.80 spid35s     AppDomain 2 (master.sys[runtime].1) created.
2019-08-01 16:18:16.11 Logon       Error: 17806, Severity: 20, State: 14.
2019-08-01 16:18:16.11 Logon       SSPI handshake failed with error code 0x8009030c, state 14 while establishing a connection with integrated security; the connection has been closed. Reason: AcceptSecurityContext failed. The Windows error code indicates the cause of failure. The logon attempt failed   [CLIENT: fe80::b45a:c99f:5a55:1fd5%12]
2019-08-01 16:18:16.11 Logon       Error: 18452, Severity: 14, State: 1.
2019-08-01 16:18:16.11 Logon       Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. [CLIENT: fe80::b45a:c99f:5a55:1fd5%12]
2019-08-01 16:18:46.07 Logon       Error: 17806, Severity: 20, State: 14.
2019-08-01 16:18:46.07 Logon       SSPI handshake failed with error code 0x8009030c, state 14 while establishing a connection with integrated security; the connection has been closed. Reason: AcceptSecurityContext failed. The Windows error code indicates the cause of failure. The logon attempt failed   [CLIENT: fe80::b45a:c99f:5a55:1fd5%12]
2019-08-01 16:18:46.07 Logon       Error: 18452, Severity: 14, State: 1.
2019-08-01 16:18:46.07 Logon       Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. [CLIENT: fe80::b45a:c99f:5a55:1fd5%12]

有人可以帮助我为我的代码建立正确的连接字符串吗?我需要IP地址吗?

3 个答案:

答案 0 :(得分:0)

我认为您可能需要在AD帐户所在的域上注册sql服务器,以使SSPI正常工作。

The error seems to be telling you that "Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. [CLIENT: fe80::b45a:c99f:5a55:1fd5%12]"

选项B是通过连接字符串中的用户名和密码使用sql身份验证。

答案 1 :(得分:0)

一个选项可能是启用混合模式进行身份验证,并使用普通用户进行连接。

您的内联文档指出:

  

下面,您可以找到连接字符串的示例。第一个使用可信连接,第二个使用SQL连接。

受信任的连接:

string connString = @"Server=INSTANCE_NAME;Database=DATABASE_NAME;Trusted_Connection = True;";

基于SQL身份验证的连接:

string connString = @"Server=INSTANCE_NAME;Database=DATABASE_NAME;User ID=USERNAME;Password=PASSWORD";
  

在此演示中,我们将使用可信连接。

Mixed_mode = true定义为使用Windows凭据,并且有一个 Microsoft resource which explains potential root causes.,但同时指出,如果您返回状态1,则应与管理员联系。

我认为这可能与身份验证模式,为sql server服务配置的服务用户或任何其他与网络相关的主题有关。调试sql server可能会让您对问题有更多了解。

但是由于错误告诉您有关Windows身份验证的信息,因此我首先尝试在连接字符串中使用用户名和密码。我希望这会有所帮助。

答案 2 :(得分:0)

如果您查看的是代码中看到的异常消息,则比尝试解析整个Sql Server日志要容易得多。但是,Sql Server日志的重要部分是:

  

该登录名来自不受信任的域,不能与Windows身份验证一起使用。

您可能正在安装Sql Server的同一VM中运行SSMS。它在那里工作是因为安装了相同的操作系统,因此集成安全性就可以了。但是您没有使用Active Directory域,并且在运行应用程序时,您是从其他系统连接的。这两个系统未设置为彼此信任,因此集成的安全身份验证将无法正常工作。

要解决此问题,您可以在相同的操作系统安装上运行所有内容(都在同一个VM中-ugh),将所有内容都放入Active Directory环境中(如果已经拥有一个,则很好,但是如果存在则重新出现)您不这样做),或者改用Sql身份验证。