简单的获取ASP准备语句

时间:2014-08-12 08:23:45

标签: sql asp-classic

我已经将PHP用于所有内容,但现在我需要在ASP中查找MS SQL中的内容。

我不能因为上帝的爱而弄清楚如何将post参数绑定到预备语句并打印结果。

我需要为每次查找只获取1行,其中SQL语句如下所示:

SELECT ID,NAME FROM MEMBERS WHERE ID =?

我已经从这个例子中得到了一个例子并阅读了一些帖子:

Response.Buffer = True
On Error Resume Next
Dim host
Dim port
Dim user
Dim password
Dim database

host = "host"
port = "1433"
user = "user"
password = "pass"
database = "database"

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")

Dim ds
ds = host & "," & port
Dim connString
connString = "Provider=SQLOLEDB;Data Source=" & ds & ";Network Library=DBMSSOCN;Initial Catalog=" & database & ";User Id=" & user & ";Password=" & password & ";"

conn.Open connString

Dim cmdPrep1 As New ADODB.Command

Set cmdPrep1.ActiveConnection = cn
cmdPrep1.CommandText = "SELECT ID,NAME FROM MEMBERS WHERE ID =?"
cmdPrep1.CommandType = adCmdText
cmdPrep1.Prepared = True

这是我的知识结束的地方。

如何将输入参数(POST)绑定到上面并对打印的行进行打印?

为什么基本的ASP示例如此难以与php相比?对我来说似乎很奇怪。

1 个答案:

答案 0 :(得分:0)

这不适用于经典的asp:

Dim cmdPrep1 As New ADODB.Command

你必须像这样使用server.createobject:

dim cmdPrep1 : set cmdPrep1 = server.createobject("ADODB.Command")

cmdPrep1.ActiveConnection = cn
cmdPrep1.CommandType = adCmdText
cmdPrep1.CommandText = "SELECT ID,NAME FROM MEMBERS WHERE ID =?"


cmdPrep1.parameters.Append cmd.createParameter( "ID", adInteger, , , Request.Form("nameOfIDField") )

dim rs : set rs = cmdPrep1.execute

现在变量rs中有ADODB.Recordset