SQL连接强制关闭
我在从 IIS7 对 Server 2008 进行 SQL 查询时遇到困难。我有一个运行更新语句的 VB.NET 类库。用于创建连接的底层代码没有改变,但突然查询在我们的测试和开发环境中失败了。但是,它仍然可以在我们的生产环境中使用稍旧的代码针对同一服务器/数据库工作。
我尝试在 web.config 中设置连接超时,但我无法解释原因。
查询的基本结构是:
Dim conn = New SqlConnection()
conn.ConnectionString = "Data Source=someserver\sqlexpress2008;Initial Catalog=DatabaseName;User ID=sa;Password=pass"
conn.Open()
Using cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "UPDATE ..."
cmd.Parameters.AddWithValue("@UName", user.name)
cmd.ExecuteNonQuery() 'fails with error
End Using
错误是:
向服务器发送请求时发生传输级错误 服务器。 (提供程序:TCP 提供程序,错误:0 - 现有连接已 被远程主机强制关闭。)
我尝试重新启动 IIS 和 SQL 服务器,但我完全没有主意。我只需要修复
I'm having difficulty with an SQL query against Server 2008 from IIS7. I have a VB.NET class library which runs an update statement. The underlying code used to create the connection hasn't changed, but suddenly the query is failing in our testing and development environments. It does, however, still work against the same server/database using the slightly older code in our production environment.
I've tried setting the connection timeout in the web.config and I'm at a loss to explain the cause.
The basic structure of the query is:
Dim conn = New SqlConnection()
conn.ConnectionString = "Data Source=someserver\sqlexpress2008;Initial Catalog=DatabaseName;User ID=sa;Password=pass"
conn.Open()
Using cmd As SqlCommand = conn.CreateCommand()
cmd.CommandText = "UPDATE ..."
cmd.Parameters.AddWithValue("@UName", user.name)
cmd.ExecuteNonQuery() 'fails with error
End Using
The error is:
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.)
I've tried restarting IIS and the SQL server and I'm totally out of ideas. I just need a fix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在调用
SqlCommand.ExecuteNonQuery()
之前打开连接。您可以通过调用SqlConnection.Open()
来完成此操作。另外,请确保您的数据库不处于单用户模式。
You need to open the connection before calling
SqlCommand.ExecuteNonQuery()
. You do this by callingSqlConnection.Open()
.Also, ensure you database isn't in single user mode.
这帮助了另一个最近陷入困境的人。您可以通过设置 SQL Server Profiler 从数据库服务器检查问题。
只需通过谷歌搜索即可找到大量有关 SQL Profiler 的信息。这是一个带有视频的网站,可能会帮助您入门。对于初学者来说,您将能够查看请求是否到达了数据库服务器。
This helped another person who was stuck recently. You could examine the problem from the database server by setting up a SQL Server Profiler.
You can find lots of info about SQL Profiler by just googling around. Here's a site with a video that might help you get started. For starters, you would be able to see if the request is even reaching the database server.
追踪这件事简直就是一场噩梦。事实证明,这是由 VB.NET 中的一个可怕的怪癖引起的。可空日期时间似乎被强制转换为
DateTime.MinValue
,这导致DateTime.MinValue
被插入到 SQLdatetime
中。修复方法是检查!property.HasValue && property.Value != DateTime.MinValue
设置命令参数时。This was a nightmare to track down. It turned out to be cause by a horrible quirk in VB.NET. Nullable datetimes seem to be coerced to
DateTime.MinValue
, which resulted in aDateTime.MinValue
being inserted into an sqldatetime
. The fix was to check for either!property.HasValue && property.Value != DateTime.MinValue
when setting the parameters for the command.这是一个网络级错误。数据库服务器由于某种原因正在终止连接。为了解决此问题,我将使用 SSMS 打开与 DEV 和 TEST 服务器的连接,并确保我可以运行简单的查询而不会出现问题。问题不太可能是您的库,因为您会遇到超时或其他类型的错误。
This is a network-level error. The database server is killing the connection for some reason. In order to troubleshoot this, I would open a connection using SSMS to the DEV and TEST servers and make sure that I can run simple queries w/o problems. It's unlikely that the issue is your library since you would be getting timeout or some other kind of errors.
正如 Lcarus 所说,数据库服务器正在出于未知原因终止连接。
您可以检查日志来验证。
C:\Program Files\Microsoft SQL Server\<您的实例>\MSSQL\LOG
日志路径将为来自 MSDN 博客的 MSDN博客
as Lcarus, said, database server is killing the connection for unknown reason.
you can check the logs, to verfiy. Log path will be
C:\Program Files\Microsoft SQL Server\<your instance>\MSSQL\LOG
from MSDN Blog MSDN Blog