Sql server 是否需要在ASP Classic中关闭嵌套的MSSQL更新命令?
使用Dreamweaver CS5,我添加了以下工作正常的服务器行为 问题是,我需要关闭MM_rsUser1吗 自动生成的代码关闭MM_rsUser,但当我试图在关闭MM_rsUser之前或之后的行上关闭MM_rsUser1时,页面失败 我发现这似乎表明我可能不“需要”,但由于这是我的第一个项目,我正在尝试学习尽可能多的“好习惯”…而且由于Dreamweaver正在生成大部分VB代码,我不想“假设”它对我来说所做的一定是今天的最佳实践。该项目正在添加动态数据和编辑说的数据到一个预先存在的经典ASP网站…我的下一个项目将升级到MVC/CSql server 是否需要在ASP Classic中关闭嵌套的MSSQL更新命令?,sql-server,asp-classic,vb6,dreamweaver,Sql Server,Asp Classic,Vb6,Dreamweaver,使用Dreamweaver CS5,我添加了以下工作正常的服务器行为 问题是,我需要关闭MM_rsUser1吗 自动生成的代码关闭MM_rsUser,但当我试图在关闭MM_rsUser之前或之后的行上关闭MM_rsUser1时,页面失败 我发现这似乎表明我可能不“需要”,但由于这是我的第一个项目,我正在尝试学习尽可能多的“好习惯”…而且由于Dreamweaver正在生成大部分VB代码,我不想“假设”它对我来说所做的一定是今天的最佳实践。该项目正在添加动态数据和编辑说的数据到一个预先存在的经典AS
<%
' *** Validate request to log in to this site.
MM_LoginAction = Request.ServerVariables("URL")
If Request.QueryString <> "" Then MM_LoginAction = MM_LoginAction + "?" + Server.HTMLEncode(Request.QueryString)
MM_valUsername = CStr(Request.Form("userid"))
If MM_valUsername <> "" Then
Dim MM_fldUserAuthorization
Dim MM_redirectLoginSuccess
Dim MM_redirectLoginFailed
Dim MM_loginSQL
Dim MM_rsUser
Dim MM_rsUser_cmd
Dim MM_loginUpdate ' used to execute timestamp to log last successful login for user
Dim MM_rsUser1 ' also used to execute timestamp as above
MM_fldUserAuthorization = "accessLevel"
MM_redirectLoginSuccess = "/sql.asp"
MM_redirectLoginFailed = "/login.asp"
MM_loginSQL = "SELECT email, password"
If MM_fldUserAuthorization <> "" Then MM_loginSQL = MM_loginSQL & "," & MM_fldUserAuthorization
MM_loginSQL = MM_loginSQL & " FROM table WHERE userid = ? AND pword = ?"
Set MM_rsUser_cmd = Server.CreateObject ("ADODB.Command")
MM_rsUser_cmd.ActiveConnection = MM_SQL_STRING
MM_rsUser_cmd.CommandText = MM_loginSQL
MM_rsUser_cmd.Parameters.Append MM_rsUser_cmd.CreateParameter("param1", 202, 1, 50, MM_valUsername) ' adVarWChar
MM_rsUser_cmd.Parameters.Append MM_rsUser_cmd.CreateParameter("param2", 202, 1, 50, Request.Form("password")) ' adVarWChar
MM_rsUser_cmd.Prepared = true
Set MM_rsUser = MM_rsUser_cmd.Execute
If Not MM_rsUser.EOF Or Not MM_rsUser.BOF Then
' username and password match - this is a valid user
Session("MM_Username") = MM_valUsername
MM_loginUpdate = "UPDATE table SET lastLoggedIn = { fn NOW() } WHERE userid = '" & MM_valUsername & "'"
MM_rsUser_cmd.CommandText = MM_loginUpdate
Set MM_rsUser1 = MM_rsUser_cmd.Execute ' unsure if I have to write an MM_rsUser1.Close somewhere or not, but page fails where I've tried
If (MM_fldUserAuthorization <> "") Then
Session("MM_UserAuthorization") = CStr(MM_rsUser.Fields.Item(MM_fldUserAuthorization).Value)
Else
Session("MM_UserAuthorization") = ""
End If
if CStr(Request.QueryString("accessdenied")) <> "" And true Then
MM_redirectLoginSuccess = Request.QueryString("accessdenied")
End If
MM_rsUser.Close
Response.Redirect(MM_redirectLoginSuccess)
End If
MM_rsUser.Close
Response.Redirect(MM_redirectLoginFailed)
End If
%>
乍一看,代码有点复杂。我看到两条语句关闭了MM_rsUser。Redirect的作用类似于return语句,因此只会执行其中一个语句,即使IF块倾向于使它看起来不一样。我认为关键在于,如果不输入if块,就无法关闭MM_rsUser1,因为它从未打开过。因此,我建议:
If Not MM_rsUser.EOF Or Not MM_rsUser.BOF Then
' username and password match - this is a valid user
Session("MM_Username") = MM_valUsername
MM_loginUpdate = "UPDATE table SET lastLoggedIn = '" & NOW() & "' WHERE userid = '" & MM_valUsername & "'"
MM_rsUser_cmd.CommandText = MM_loginUpdate
Set MM_rsUser1 = MM_rsUser_cmd.Execute ' unsure if I have to write an MM_rsUser1.Close somewhere or not, but page fails where I've tried
If (MM_fldUserAuthorization <> "") Then
Session("MM_UserAuthorization") = CStr(MM_rsUser.Fields.Item(MM_fldUserAuthorization).Value)
Else
Session("MM_UserAuthorization") = ""
End If
if CStr(Request.QueryString("accessdenied")) <> "" And true Then
MM_redirectLoginSuccess = Request.QueryString("accessdenied")
End If
MM_rsUser1.Close 'close it here
MM_rsUser.Close
Response.Redirect(MM_redirectLoginSuccess)
End If 'Not MM_rsUser.EOF Or Not MM_rsUser.BOF
'not open, so don't close it
MM_rsUser.Close
Response.Redirect(MM_redirectLoginFailed)
End If 'MM_valUsername <> ""
到
乍一看,代码有点复杂。我看到两条语句关闭了MM_rsUser。Redirect的作用类似于return语句,因此只会执行其中一个语句,即使IF块倾向于使它看起来不一样。我认为关键在于,如果不输入if块,就无法关闭MM_rsUser1,因为它从未打开过。因此,我建议:
If Not MM_rsUser.EOF Or Not MM_rsUser.BOF Then
' username and password match - this is a valid user
Session("MM_Username") = MM_valUsername
MM_loginUpdate = "UPDATE table SET lastLoggedIn = '" & NOW() & "' WHERE userid = '" & MM_valUsername & "'"
MM_rsUser_cmd.CommandText = MM_loginUpdate
Set MM_rsUser1 = MM_rsUser_cmd.Execute ' unsure if I have to write an MM_rsUser1.Close somewhere or not, but page fails where I've tried
If (MM_fldUserAuthorization <> "") Then
Session("MM_UserAuthorization") = CStr(MM_rsUser.Fields.Item(MM_fldUserAuthorization).Value)
Else
Session("MM_UserAuthorization") = ""
End If
if CStr(Request.QueryString("accessdenied")) <> "" And true Then
MM_redirectLoginSuccess = Request.QueryString("accessdenied")
End If
MM_rsUser1.Close 'close it here
MM_rsUser.Close
Response.Redirect(MM_redirectLoginSuccess)
End If 'Not MM_rsUser.EOF Or Not MM_rsUser.BOF
'not open, so don't close it
MM_rsUser.Close
Response.Redirect(MM_redirectLoginFailed)
End If 'MM_valUsername <> ""
到
谢谢你的回复,大卫。不幸的是,我试着在你建议的地方关闭它。很抱歉,我的第一个问题不清楚……忘了,MM_rsUser也被关闭了。还有其他想法吗?+1用于尝试学习好习惯,以及计划迁移到MVC/C。在不了解页面细节的情况下,我无法提出更多建议。确保您测试了所有3种情况:1没有输入用户ID;2.输入了用户ID MM_ValuerName和密码,但在查找中未找到;3良好的用户ID和密码。我又看到了一件闻起来很好笑的事;我会更新我的答案。我试着做一些类似于你建议的事情,基本上是复制/粘贴第一批MMrsuser代码,但是页面根本不会加载。我对“重用”MM_reUser_cmd.Execute的思考过程是,在我将上面提到的两行放在更新中时,我“认为”由于连接已经打开,并且已经处理了第一条SQL语句,我可以通过将语句从MM_LoginSQL更改为MM_loginUpdate来重用它。如果在MM_fldUserAuthorization之前放置了正确的用户/密码,则页面将失败。在任何情况下,您都必须为MM_loginUpdate设置上次登录日期和用户的参数。将这样做,谢谢!我不知道我必须为上次更新设置参数…我确实错过了一个用户的参数。我猜我想既然我没有把?在声明中,我不需要这样做。谢谢谢谢你的回复,大卫。不幸的是,我试着在你建议的地方关闭它。很抱歉,我的第一个问题不清楚……忘了,MM_rsUser也被关闭了。还有其他想法吗?+1用于尝试学习好习惯,以及计划迁移到MVC/C。在不了解页面细节的情况下,我无法提出更多建议。确保您测试了所有3种情况:1没有输入用户ID;2.输入了用户ID MM_ValuerName和密码,但在查找中未找到;3良好的用户ID和密码。我又看到了一件闻起来很好笑的事;我会更新我的答案。我试着做一些类似于你建议的事情,基本上是复制/粘贴第一批MMrsuser代码,但是页面根本不会加载。我对“重用”MM_reUser_cmd.Execute的思考过程是,在我将上面提到的两行放在更新中时,我“认为”由于连接已经打开,并且已经处理了第一条SQL语句,我可以通过将语句从MM_LoginSQL更改为MM_loginUpdate来重用它。如果在MM_fldUserAuthorization之前放置了正确的用户/密码,则页面将失败。在任何情况下,您都必须为MM_loginUpdate设置上次登录日期和用户的参数。将这样做,谢谢!我不知道我必须为上次更新设置参数…我确实错过了一个用户的参数。我猜我想既然我没有把?在声明中,我不需要这样做。谢谢
Dim MM_rsUser_cmd1
Set MM_rsUser_cmd1 = Server.CreateObject ("ADODB.Command")
MM_rsUser_cmd1.ActiveConnection = MM_SQL_STRING
MM_rsUser_cmd1.CommandText = MM_loginUpdate
MM_rsUser_cmd1.Parameters.Append MM_rsUser_cmd1.CreateParameter("param1", 135, 1, -1, NOW()) ' adDBTimeStamp
MM_rsUser_cmd1.Parameters.Append MM_rsUser_cmd1.CreateParameter("param2", 202, 1, 50, MM_valUsername) ' adVarWChar
MM_rsUser_cmd1.Prepared = true
Set MM_rsUser1 = MM_rsUser_cmd1.Execute