You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Studio中SQL单元测试遇System.AccessViolationException求助

SQL Server单元测试在Visual Studio中运行时触发System.AccessViolationException错误

我在Visual Studio中为SQL Server应用实现单元测试,运行测试资源管理器中的测试时,触发如下初始化异常:

初始化方法ModulverbinderDB_UnitTest.TestISOValidateStepV2_MockError.TestInitialize抛出异常。System.AccessViolationException: 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。

堆栈跟踪:

SNINativeManagedWrapperX64.SNIOpenSyncExWrapper(SNI_CLIENT_CONSUMER_INFO& pClientConsumerInfo, IntPtr& ppConn)
SNINativeMethodWrapper.SNIOpenSyncEx(ConsumerInfo consumerInfo, String constring, IntPtr& pConn, Byte[] spnBuffer, Byte[] instanceName, Boolean fOverrideCache, Boolean fSync, Int32 timeout, Boolean fParallel, Int32 transparentNetworkResolutionStateNo, Int32 totalTimeout, Boolean isAzureSqlServerEndpoint, SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo)
SNIHandle.ctor(ConsumerInfo myInfo, String serverName, Byte[] spnBuffer, Boolean ignoreSniOpenTimeout, Int32 timeout, Byte[]& instanceName, Boolean flushCache, Boolean fSync, Boolean fParallel, TransparentNetworkResolutionState transparentNetworkResolutionState, Int32 totalTimeout, SqlConnectionIPAddressPreference ipPreference, SQLDNSInfo cachedDNSInfo)
TdsParserStateObject.CreatePhysicalSNIHandle(String serverName, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Byte[]& instanceName, Byte[] spnBuffer, Boolean flushCache, Boolean async, Boolean fParallel, TransparentNetworkResolutionState transparentNetworkResolutionState, Int32 totalTimeout, SqlConnectionIPAddressPreference ipPreference, String cachedFQDN)
TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover, Boolean isFirstTransparentAttempt, SqlAuthenticationMethod authType, String certificate, ServerCertificateValidationCallback serverCallback, ClientCertificateRetrievalCallback clientCallback, Boolean useOriginalAddressInfo, Boolean disableTnir)
SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover, Boolean isFirstTransparentAttempt, Boolean disableTnir)
SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
SqlInternalConnectionTds.ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, ServerCertificateValidationCallback serverCallback, ClientCertificateRetrievalCallback clientCallback, DbConnectionPool pool, String accessToken, SqlClientOriginalNetworkAddressInfo originalNetworkAddressInfo, Boolean applyTransientFaultHandling)
SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
SqlConnection.TryOpen(TaskCompletionSource`1 retry, SqlConnectionOverrides overrides)
SqlConnection.Open(SqlConnectionOverrides overrides)
SqlDatabaseTestService.CreateConnectionContext(ConnectionContextElement config)
SqlDatabaseTestService.OpenExecutionContext()
SqlDatabaseTestClass.InitializeTest()
TestISOValidateStepV2_MockError.TestInitialize() line 24

已排查主机VM和Visual Studio均为x64架构,排除x86/x64不兼容问题,寻求解决办法。


可能的解决办法

1. 更新SQL Client相关库

  • 检查项目引用的System.Data.SqlClient或Microsoft.Data.SqlClient版本,升级至最新稳定版,旧版SNI组件可能存在内存访问bug。
  • 若使用SQL Server Native Client,卸载旧版本后安装最新的ODBC驱动或MSOLEDBSQL驱动。

2. 禁用透明网络解析

在数据库连接字符串中添加TransparentNetworkResolution=false参数,强制客户端直接连接指定服务器地址,规避TNR相关内存异常。

3. 确认测试项目平台目标

右键测试项目 → 属性 → 生成 → 平台目标,选择x64,同时取消勾选“首选32位”选项,清理解决方案后重新生成。

4. 重置Visual Studio测试缓存

关闭Visual Studio,删除项目目录下的.vs隐藏文件夹,清除测试缓存后重新打开项目运行测试。

5. 切换身份验证方式测试

若使用集成身份验证,尝试切换为SQL Server身份验证,排除Windows身份验证相关的内存访问问题。

6. 修复或更新Visual Studio

运行Visual Studio安装程序选择“修复”选项,修复损坏组件;同时安装最新更新补丁,确保测试工具链处于最新状态。


内容的提问来源于stack exchange,提问作者SebUhb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 07:15:46