关闭连接 [英] Closing a connection

查看:83
本文介绍了关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我绝对坚信!



为什么,为什么关闭连接挂起我的托管服务器???



快速背景:我通过将步骤写入服务器上的文本文件来输入代码来测试每一步。 (顺便说一句,所有的文本编写代码都可以正常工作。)



当我关闭连接时(如下图所示),网站会挂起大约60秒钟最终抛出连接被重置页面。



不知道这里发生了什么?



我的代码:



This is making me absolutely NUTS!

Why, oh why is closing a connection hanging up my hosting server???

Quick background: I put in code to test every single step by writing out the step to a text file on the server. (All of the text-writing code works fine by the way.)

When I close the connection (as shown below) the site hangs up for about 60 seconds and eventually throws a "connection was reset" page.

Any idea what's going on here??

My code:

' start by verifying that this record is still available first
Dim conn As OleDbConnection = New OleDbConnection(sConnectionString)
Dim cmd As OleDbCommand = New OleDbCommand()
Dim sql As String = "SELECT [Type] FROM [Login] WHERE [UserID] = '" & sUsrID & "'"

Try
	cmd = New OleDbCommand(sql, conn)
	conn.Open()
Catch ex As Exception
	If HandleError(ex, "sql: " & sql) Then Server.Transfer("~/errorhandler.aspx")
End Try

Dim sdr As OleDbDataReader = cmd.ExecuteReader(Data.CommandBehavior.SingleRow)
Dim bFound As Boolean = sdr.Read()
Dim iTyp As Integer
If bFound Then iTyp = sdr("Type")

sdr.Close()
conn.Close()   <--- THIS causes the site to hang up!





'以下代码永远不会执行:

errRoutineName =注册:btnSubmit_Click(2)



' The following code NEVER executes:
errRoutineName = "Register:btnSubmit_Click(2)"

推荐答案

尝试使用块包装连接和命令对象,请参阅: http://msdn.microsoft.com/ en-us / library / ms254507(v = vs.110).aspx?cs-save-lang = 1& cs-lang = vb#code-snippet-1 [ ^ ]



此外,您应该使用ExecuteScalar而不是ExecuteReader,这样您就没有使用DataReader对象。如果ExecuteScalar返回null,则找不到用户,如果它没有返回null,只需将其强制转换为类型对象。



使用Using块时,您不必担心清理资源,框架会为您完成。我不确定你为什么会遇到这个问题,但取出阅读器并使用使用块会清理你的代码。



此外,因为我可以在在此框中,请勿对查询使用字符串连接。如果我在你的用户名框中输入a'; DROP TABLE [登录]; - ,我会删除你的登录表。了解如何在查询中使用参数,使您的站点免受SQL注入攻击。
Try wrapping your connection and command objects in Using blocks, see: http://msdn.microsoft.com/en-us/library/ms254507(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1[^]

Also, you should probably use ExecuteScalar instead of ExecuteReader, that way you don't have to use a DataReader object. If the ExecuteScalar returns null, then the user is not found, if it doesn't return null, just cast it to your type object.

When using the Using blocks, you don't have to worry about cleaning up the resources, the framework does it for you. I'm not sure why you are getting the issue, but taking out the Reader and using Using blocks will clean up your code.

Also, since I can preach in this box, do not use string concatenation with your queries. If I were to type "a'; DROP TABLE [Login]; --" in your user name box, I would delete your login table. Learn how to use parameters with your queries to make your site safe from SQL Injection attacks.


这篇关于关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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