2012-05-10 2 views
2

Dreamweaver CS5 사용 다음과 같은 서버 동작을 추가했습니다.ASP Classic에서 중첩 MSSQL 업데이트 명령을 닫아야합니까?

질문 : MM_rsUser1을 닫아야합니까?

자동 생성 된 코드는 MM_rsUser를 닫지 만 MM_rsUser가 닫히기 전이나 후에 줄의 MM_rsUser1을 닫으려고하면 페이지가 실패합니다.

내가 '필요 없다'는 것을 나타내는 것으로 보이는이 reference for MySql이 첫 번째 프로젝트이므로 가능한 한 많은 '좋은 습관'을 배우려고 노력하고 있으며 Dreamweaver가 많이 생성하고 있기 때문에 VB 코드 중, 나는 그것이 나를 위해하는 일이 필연적으로 오늘날 가장 좋은 방법이라고 가정하고 싶지 않습니다. 처음에,

<% 
' *** 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 
%> 

답변

0

코드가 조금 까다 롭습니다 (사전 exising 클래식 ASP 사이트에 데이터를 프로젝트는 동적 데이터를 추가하고 편집 말한 것은 ... 내 옆에있는 프로젝트는 MVC/C 번호로 업그레이드 예정) 보기. 나는 MM_rsUser를 닫는 2 개의 명령문을 본다. Response.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_cmd 기껏 혼란, 그리고 몇 가지 오류의 원인이 될 수 있습니다. 응답 다윗

MM_rsUser_cmd.CommandText = MM_loginUpdate 
Set MM_rsUser1 = MM_rsUser_cmd.Execute 

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 
+1

감사를 변경합니다. 불행히도, 나는 당신이 제안한 장소에서 그것을 닫으려고했습니다. (제 첫 질문에서 분명하지 않은 것에 대해 유감스럽게 생각합니다 ... MM_rsUser도 더 아래로 닫혔습니다). 다른 의견은 있습니까? –

+1

+1 좋은 습관을 배우고 MVC/C#으로 이동할 계획이었습니다. "페이지가 실패합니다."라는 내용을 모른 채 제안 할 수있는 것은 많지 않습니다. 다음 3 가지 경우 모두 테스트해야합니다. (1) 입력 된 사용자 ID가 없습니다. (2) 사용자 ID (MM_valUsername) 및 암호는 입력되었지만 조회에서 찾을 수 없습니다. (3) 좋은 사용자 ID 및 암호. 그리고 나는 우스꽝스럽게 냄새를 맡는 또 하나의 것을 본다. 나는 나의 대답을 업데이트 할 것이다. –

+0

나는 기본적으로 MM_rsUser에 대한 코드의 첫 번째 배치를 복사/붙여 넣기하여 기본적으로 제안한 것과 비슷한 작업을 시도했지만 페이지가 전혀로드되지 않습니다. MM_reUser_cmd.Execute를 '재사용'하는 과정에서 생각한 것은 위에서 언급 한 두 줄을 업데이트에 배치 한 시점에서 연결이 이미 열려 있고 이미 첫 번째 SQL 문을 처리했기 때문에 ' 그 문장을 MM_LoginSQL에서 MM_loginUpdate로 변경하여 재사용 할 수 있습니다. 앞에 올 때 올바른 사용자/암호를 사용한 후에 만 ​​페이지가 실패 함 If (MM_fldUserAuthorization <> "") –

관련 문제