Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 什么是;“重置连接”;什么意思?System.Data.SqlClient.SqlException(0x80131904)_C#_.net_Sql Server_Sqlconnection_Dbup - Fatal编程技术网

C# 什么是;“重置连接”;什么意思?System.Data.SqlClient.SqlException(0x80131904)

C# 什么是;“重置连接”;什么意思?System.Data.SqlClient.SqlException(0x80131904),c#,.net,sql-server,sqlconnection,dbup,C#,.net,Sql Server,Sqlconnection,Dbup,我知道这有点含糊不清,但我无法准确指出问题所在 当我对本地数据库运行一些代码时,它运行得很好。当我使用远程数据库时,我得到一个错误。 它发生在程序执行的中途。DBup升级运行,然后手动查询失败,出现以下异常: System.Data.SqlClient.SqlException(0x80131904):重置连接会导致与初始登录不同的状态。登录失败。 用户“sa”登录失败 我正在使用newsqlconnection()手动创建SqlConnection,我也在使用 我不确定我会做错什么。也不知道从

我知道这有点含糊不清,但我无法准确指出问题所在

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

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

我正在使用
newsqlconnection()
手动创建
SqlConnection
,我也在使用

我不确定我会做错什么。也不知道从哪里开始调试。
ConnectionString
不会更改,我总是使用
sa
连接到数据库


一个好问题是“重置连接”是什么意思?我是怎么做到的?

这不是我问题的答案,但我解决了这个问题,没有处理
SqlConnection
,而是重用它


我仍然不理解这个问题

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

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

有关,请参见。这是否解决了您的问题?如果数据库连接到服务器,请不要在连接字符串中使用AttachDB(localdb)属性(删除)。服务器拥有mdf数据库文件,不允许用户直接访问该文件。您必须通过服务器和实例连接到数据库。要进行调试,我使用SQL server Management Studio。SSMS登录窗口将显示服务器和实例名称,应在c连接字符串中使用。登录窗口还显示窗口c连接字符串中的redental相当于Integrated Security=true,并使用用户(或组)windows凭据。@I或解决方案是使用普通ado.net,我正在执行此操作。@jdweng我没有使用(localdb)它只是一个在LocalHost上运行的SQLServer实例,要使连接正常工作,本地计算机和远程计算机必须具有相同的凭据。通常我会将数据库凭据设置为一个窗口用户组,然后将用户放入该组。必须在本地和远程计算机上分配该组。公司通常有组策略可用于数据库凭据的设置。这几乎与使用
sa
一样糟糕。您现在正在使用的任何表上累积锁,阻止其他连接。您尚未发布任何代码,甚至没有发布完整的异常,因此无法理解问题。但这表明,您用于创建c连接有点奇怪。这两个连接字符串看起来像什么?您使用的SQL Server版本和版本是什么?ADO.NET将连接池化并重用它们。每次关闭/处置连接时,都会重置连接并将其放回池中。如果Windows身份验证为u,则连接将由连接字符串和Windows帐户池化一个关于重置连接的错误表明池混淆了(不太可能,否则你会看到很多类似的问题)或者远程服务器发生了变化。可能是不同的数据库?切换到单用户模式?用户实例数据库?@PanagiotisKanavos感谢您的评论。我没有发布代码,因为它是一个很大的代码库,我不确定是什么导致了问题。创建连接的代码与新的SqlConnection一样简单(connectionString);然后是Open()我建议再次阅读可能的副本,因为您似乎在做完全相同的事情—创建一个新数据库。副本使用EF迁移,而您使用的是DbUp。这意味着连接到同一服务器可能会在一个阶段到下一个阶段尝试登录到不同的数据库。这可能是一个新问题,举个简单的例子e是最好的。。。