Simple fetch ASP prepared statement

霸气de小男生 提交于 2019-12-25 18:45:24

问题


I've used php for everything, but now I need to look up something in MS SQL with ASP.

I cannot for the love of God figure out how to bind post parameters to a prepared statement and print the results.

I need to fetch only 1 row for each lookup, where the SQL statement would look like:

SELECT ID,NAME FROM MEMBERS WHERE ID = ?

I've gotten as far as this, from an example and reading a bit of posts:

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

This is where my knowledge ends.

How would I bind input paramters (POST) to the above and do a print of the fetched row?

Why are basic ASP examples so hard to come by vs. php? Seems odd to me.


回答1:


this will not work in classic asp:

Dim cmdPrep1 As New ADODB.Command

you have to use server.createobject like so:

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

now you have an ADODB.Recordset in your variable rs.



来源:https://stackoverflow.com/questions/25259434/simple-fetch-asp-prepared-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!