将 SQL 变量传递给 ASP [英] Passing SQL variable to ASP

查看:19
本文介绍了将 SQL 变量传递给 ASP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里只是一个语法问题.到目前为止,我正在使用 SQL 和 ASP 并拥有此代码:

Just a bit of a syntax problem here. I'm using SQL and ASP and have this code thus far:

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

我如何将 SQL 变量 @testID 传递给 ASP,以便它可以写入 response.write

How would I pass the SQL variable @testID to ASP so that it could be written within the response.write

例子:

Dim, ASPID
ASPID = @testID
Response.write(ASPID)

(显然这是错误的)谁能告诉我该怎么做?对不起,如果这对某些人来说有点太简单了:P

(obviously this is wrong) Could anyone tell me how to do this? Sorry if this is a bit too simple for some people :P

推荐答案

试试这个;

' 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

未经测试,但我倾向于这样做,而不必担心处理撇号和恶意代码.

Not tested but this is how I tend to do this without having to worry about handling apostrophes and malicious code.

这篇关于将 SQL 变量传递给 ASP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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