0

我们正在使用 SQLLocalDB 连接到本地数据库。无需网络访问 - C# 客户端应用程序与 SQLLocalDB 位于同一台计算机上。有时我会收到以下错误(似乎只发生在某些 PC 上):

Microsoft.SqlServer.Management.Common.ConnectionFailureException: Failed to connect to server (localdb)\myserverinstancename. ---> System.Data.SqlClient.SqlException: Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=27888; handshake=0; ---> System.ComponentModel.Win32Exception: The wait operation timed out

奇怪的是,如果 PC 处于离线状态,并且如果连接到任何类型的互联网连接并重试,问题就会消失。但是,在大多数情况下,即使没有连接,连接也能正常工作。

我发现最接近我的问题的是:Connection to SQL Server Works有时

但这是在讨论完整的 SQL Server,而不是 SQLLocalDB,并且该解决方案使用“Sql Server 配置管理器”,它不适用于 SQLLocalDB。

我的问题:

  • SQLLocalDb 是否出于某种原因使用 IPv6/TCP,即使在同一台 PC 上也是如此?
  • 有没有办法更改 SQLLocalDB 的这些设置?
  • 或者这个问题有不同的原因吗?

编辑:在下面一些评论的帮助下,我意识到失败的那一行是它调用数据库创建脚本时。最初的连接尝试似乎有效,但是当我运行我的创建脚本时,它会抛出“无法连接到服务器”。

/// This line works
var server = new Server("(localdb)\myserverinstancename");

/// this line works 99% of the time, but sometimes "failed to connect to server"
server.ConnectionContext.ExecuteNonQuery(databaseCreationScript);

4

0 回答 0