2014-02-07 2 views
0

여기에 약간의 구문 문제가 있습니다. 나는 SQL과 ASP를 사용하여 지금까지이 코드를 가지고있어 :SQL 변수를 ASP에 전달

Dim, ASPID 
ASPID = @testID 
Response.write(ASPID) 
: 그것이 response.write

예에서 쓸 수 있도록 내가 ASP에 SQL 변수 @testID을 통과 할 방법

set rs=Server.CreateObject("ADODB.recordset") 
rs.Open "Select * from Questions", conn 

sql="INSERT INTO Questions(QuestionText, QuestionType)" 
sql=sql & " VALUES " 
sql=sql & "('" & qtext & "'," 
sql=sql & "'" & "checkbox" & "');" 
sql=sql & "DECLARE @testID int;" 
sql=sql & "SET @testID = @@identity;" 

on error resume next 
conn.Execute sql,recaffected 
if err<>0 then 
    Response.Write("An Error Has Occured") 
else 

end if 

(분명히 잘못되었습니다.) 누구든지이 작업을 수행하는 방법을 알려주시겠습니까? 죄송합니다. 일부 사람들에게는 너무 단순합니다. P

+0

의 중복 가능성 http://stackoverflow.com/questions/21626338/sql- 청소기는 이와 같은 함수를 사용 될 with-asp-syntax) – Lankymart

답변

2

시도해보십시오.

' What is this for? 
'set rs=Server.CreateObject("ADODB.recordset") 
'rs.Open "Select * from Questions", conn 

Dim cmd, rs, sql, new_id, yourconnstring 

sql = "" 
sql = sql & "INSERT INTO Questions(QuestionText, QuestionType) VALUES (?, ?);" & vbCrLf 
sql = sql & "SELECT SCOPE_IDENTITY();" 

Set cmd = Server.CreateObject("ADODB.Command") 
With cmd 
    .ActiveConnection = yourconnstring 'No need for separate connection object 
    .CommandType = adCmdText 
    .CommandText = sql 
    .Parameters.Append(.CreateParameter("@questiontext", adVarWChar, adParamInput, 255) 
    .Parameters.Append(.CreateParameter("@questiontype", adVarWChar, adParamInput, 255) 
    Set rs = .Execute(, Array(qtext, "checkbox")) 
    If Not rs.EOF Then new_id = rs(0) 
    Call rs.Close() 
    Set rs = Nothing 
End With 
Set cmd = Nothing 

If Len(new_id) < 1 Then 
    Call Response.Write("An Error Has Occured") 
Else 
    Call Response.Write("ID is " & new_id) 
End If 

테스트를 거치지 않았지만 어 포스 트로피 및 악의적 인 코드를 처리하는 것에 대해 걱정할 필요가없는 방법입니다. 당신이 "qtext"를 통해 사용자 입력에서 선택하지 않은 내용을 전달하지 않는 명령 객체

Set rs=Server.CreateObject("ADODB.recordset") 
rs.Open "Select * from Questions", conn 
... 
rs.Close 
sql="INSERT INTO Questions(QuestionText, QuestionType)" & _ 
    " VALUES " & _ 
    "('" & qtext & "'," & _ 
    "'" & "checkbox" & "');" & _ 
    "SELECT SCOPE_IDENTITY() AS testid;" 

On Error Resume Next 
rs.Open sql, conn 
recaffected = rs("testid") 
rs.Close : Set rs = Nothing 
If err.Number <> 0 Then 
    Response.Write("An Error Has Occured") 
Else 
End If 
On Error Goto 0 

를 사용하지 않고

-1

대안이 분명하다 그냥

Replace(qtext,"'","''") 

충분하지 않을 수 있습니다 .

Function SQLScrub(ByVal val) 
    dim HostileAttemptString(5) 
     HostileAttemptString(0) = "EXEC(@" 
     HostileAttemptString(1) = "exec(@" 
     HostileAttemptString(2) = "and%201=" 
     HostileAttemptString(3) = "and%20char(" 
     HostileAttemptString(4) = "exec%[email protected]" 
     HostileAttemptString(5) = "EXEC%[email protected]" 

    dim str,rtnVal 
    rtnVal = val 
    rtnVal = Replace(val,"'","''") 

    For Each str in HostileAttemptString 
    rtnVal = Replace(rtnVal,str,"") 
    Next 

    SQLScrub = rtnVal 
end function 

구문 :

qtext = SQLScrub(original_userinput) 
[ASP 구문 SQL (
+0

저에게 오류가 발생했습니다. – howdybaby

+0

@Lankymart 편집 된 답변보기 –

+0

@Lankymart ADODB.Command 객체를 사용하는 것이 더 깔끔하다는 데 동의합니다. 내가 개체를 살균에서 빌드를 신뢰하지, 그 이유는 내가 내 자신의 .ASP 코드를 항상 ADODB.Command 개체와 사용자 정의 ""내가 문자열 배열을 전역 변수로 선언 유일한 차이와 함께 사용하고 있습니다 :) –