“重置连接"是什么意思是什么意思?System.Data.SqlClient.SqlException (0x80131904) [英] What does "Resetting the connection" mean? System.Data.SqlClient.SqlException (0x80131904)

查看:94
本文介绍了“重置连接"是什么意思是什么意思?System.Data.SqlClient.SqlException (0x80131904)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这有点含糊,但我无法确定问题所在.

I know this is a bit vague, but I'm not being able to pinpoint the issue.

当我对本地数据库运行一些代码时,它运行良好.当我使用远程数据库时,出现错误.它发生在程序执行的中途.DBup 升级运行,然后手动查询失败,出现此异常:

When I run the a bit of code against a local database it runs fine. When I use a remote database I get an error. It occurs midway of the program execution. The DBup upgrade runs, and then a manual query fails with this exception:

System.Data.SqlClient.SqlException (0x80131904):重置连接导致与初始登录不同的状态.登录失败.用户sa"登录失败.

System.Data.SqlClient.SqlException (0x80131904): Resetting the connection results in a different state than the initial login. The login fails. Login failed for user 'sa'.

我正在使用 new SqlConnection() 手动创建 SqlConnection 并且我也在使用 DbUp

I'm creating SqlConnection manually using new SqlConnection() and I'm also using DbUp

我不确定我做错了什么.也不从哪里开始调试这个.ConnectionString 不会改变,我总是使用 sa 连接到数据库.

I'm not sure what I can be doing wrong. Nor where to start debugging this. The ConnectionString does not change and I'm always using sa to connect to the database.

一个很好的问题是什么是重置连接"?是什么意思?我是怎么做的?

A good question to start is What does "Resetting the connection" mean? How am I doing it?

推荐答案

经过几个小时的反复试验,我得到了重现错误的最小代码片段

After a couple of hours of trial and error I got to a minimal piece of code that reproduces the error

string dbName = "TESTDB";
Run("master", $"CREATE DATABASE [{dbName}]");
Run(dbName, $"ALTER DATABASE [{dbName}] COLLATE Latin1_General_100_CI_AS");
Run(dbName, "PRINT 'HELLO'");

void Run(string catalog, string script)
{
    var cnxStr = new SqlConnectionStringBuilder
    {
        DataSource = serverAndInstance,
        UserID = user,
        Password = password,
        InitialCatalog = catalog
    };

    using var cn = new SqlConnection(cnxStr.ToString());
    using var cm = cn.CreateCommand();
    cn.Open();
    cm.CommandText = script;
    cm.ExecuteNonQuery();
}

完整的堆栈跟踪是

Unhandled Exception: System.Data.SqlClient.SqlException: Resetting the connection results in a different state than the initial login. The login fails.
Login failed for user 'user'.
Cannot continue the execution because the session is in the kill state.
A severe error occurred on the current command.  The results, if any, should be discarded.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
...

如果我将第一个 Run(dbName... 更改为 Run("master"...,它运行良好.所以它与在同一个数据库的上下文中运行ALTER DATABASE有关

If I change the first Run(dbName... to Run("master"... it runs fine. So it's related to running ALTER DATABASE in the context of the same database

这篇关于“重置连接"是什么意思是什么意思?System.Data.SqlClient.SqlException (0x80131904)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆