0

在asp服务器中,执行ExecuteScalarExecuteReaderExecuteNonQuery后,我们得到SQL Server错误,如下所示。错误只会有时会出现,并不总是会出现。所以有人知道为什么会出现这个错误?如何解决它?在asp服务器中SQL Server连接的错误

System.Data.SqlClient.SqlException: A transport-level error has occurred when sending the request to the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.)

at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
at System.Data.SqlClient.TdsParserStateObject.WriteSni()
at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode)
at System.Data.SqlClient.TdsParserStateObject.ExecuteFlush()
at System.Data.SqlClient.TdsParser.TdsExecuteSQLBatch(String text, Int32 timeout, SqlNotificationRequest notificationRequest, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
at System.Data.SqlClient.SqlCommand.ExecuteReader()
at WinMsg.Page_Load(Object sender, EventArgs e)

+0

请确保您有有效的连接字符串。和访问权限。 – 2013-03-15 05:46:02

+0

@Ravi他说他们在成功调用数据库后得到这个错误。我无法想象他们正在改变连接字符串和访问权限。 – jadarnel27 2013-03-15 05:58:05

+0

连接字符串是正确的,因为错误只有几次。 – 2013-03-15 06:10:50

回答

0

您提到了DataReader:请确保您正确关闭它。我怀疑它仍然是开放的,并阻止进一步的数据库操作。

MSDN

While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader. While in this state, no other operations can be performed on the SqlConnection other than closing it. This is the case until you call the SqlDataReader.Close method. If the SqlDataReader is created with CommandBehavior set to CloseConnection, closing the SqlDataReader closes the connection automatically.

也请确保SqlConnection对象被关闭,使用后妥善处置。