我有以下经典的asp工作,在数据库中调用存储过程。我想转换它,而不是调用它在sql中传递的存储过程,然后添加prams然后执行它。
我尝试了各种各样的东西,但却无法正常工作。 有人可以转换这个例子吗?
使用params传入sql时,似乎很难得到合理的错误消息。有没有办法看到这个调用的痕迹,所以我们可以知道导致问题的原因是什么?
Set cmd = server.createobject("ADODB.Command")
cmd.ActiveConnection = theDatabase
cmd.CommandType = adCmdStoredProc
cmd.commandText = "AddResponse"
cmd.Parameters("@id") = id
cmd.Parameters("@pc") = pc
cmd.Parameters("@idDate") = now
cmd.Execute , , adExecuteNoRecords
set cmd = Nothing
存储过程定义
ALTER PROCEDURE [dbo].[AddResponse] @id NVARCHAR(12), @pc NVARCHAR(12), @idDate datetime
AS
BEGIN
SET NOCOUNT ON;
select * from EmailResponse
if not exists (SELECT id, projectCode FROM EmailResponse WHERE id = @id and projectCode = @pc)
begin
INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(@id, @pc, @idDate)
end
END
编辑:
以下是我对每个人的回答
哇stackoverflow是伟大的,因为每个人都喜欢你自己
谁花了一点时间帮助别人。
选择*是一个错误
我必须维护并将一些旧的asp代码转换为使用存储过程
存储过程是最好的方式"大多数"当时
由于各种原因,有时最好在代码中使用sql
(快速测试和开发,无法访问数据库等)
所以我需要知道如何处理这两种方式。
cmd.Parameters.Refresh
没有这个电话我的代码工作正常
真的有必要吗?
阅读它应该做的事情并不是我需要使用它的很多帮助
了解类型对于所有类型的编程都至关重要
这正是我要求的更多
Carl Prothman - 数据类型映射
谢谢你!
我还想知道如何设置记录集对象,即使我忘了问。也谢谢你!
set rs = server.createObject(" adodb.recordset")
rs = - cmd.Execute
我三个都在工作。 对于任何感兴趣的人来说,工作和测试代码来显示所有三种方法。
' Stored proc example
' ------------------------------------------
dim theDatabase, cmd, id, pc
theDatabase = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"
id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))
if pc<>"" and id<>"" then
Set cmd = server.createobject("ADODB.Command")
cmd.ActiveConnection = theDatabase
cmd.CommandType = adCmdStoredProc
cmd.commandText = "AddResponse"
cmd.Parameters("@id") = id
cmd.Parameters("@pc") = pc
cmd.Parameters("@idDate") = now
cmd.Execute , , adExecuteNoRecords
set cmd = Nothing
end if
' Inline SQl with ? example
' ------------------------------------------
dim theDatabase, cmd, id, pc
theDatabase = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"
id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))
if pc<>"" and id<>"" then
Set cmd = server.createobject("ADODB.Command")
cmd.ActiveConnection = theDatabase
cmd.CommandType = adCmdText
cmd.CommandText = _
"if not exists (SELECT id, projectCode FROM EmailResponse WHERE id = ? and projectCode = ?)" &_
"begin INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(?, ?, ?) end "
cmd.Parameters.Append cmd.CreateParameter("@id", adInteger, adParamInput, , id)
cmd.Parameters.Append cmd.CreateParameter("@pc", adVarchar, adParamInput, 12, pc)
cmd.Parameters.Append cmd.CreateParameter("@id2", adInteger, adParamInput, , id)
cmd.Parameters.Append cmd.CreateParameter("@pc2", adVarchar, adParamInput, 12, pc)
cmd.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, -1, now)
cmd.Execute , , adExecuteNoRecords
set cmd = Nothing
end if
' Inline SQl with @ example
' ------------------------------------------
dim theDatabase, cmd, sql, id, pc
theDatabase = "Driver={SQL Server}; Server=10.10.10.10,1433; Database=Test; uid=TestUser; pwd=TestPass;"
id = cleanInt(request.querystring("id"))
pc = sqlSafe(clean(request.querystring("pc")))
if pc<>"" and id<>"" then
Set cmd = server.createobject("ADODB.Command")
sql = ""
sql = sql & "SET NOCOUNT ON;" & vbCrLf
sql = sql & "DECLARE @id NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @pc NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @idDate DATETIME" & vbCrLf
sql = sql & "SELECT @id = ?, @pc = ?, @idDate = ?" & vbCrLf
sql = sql & "IF NOT EXISTS (SELECT id, projectCode FROM EmailResponse WHERE id = @id and projectCode = @pc)" & vbCrLf
sql = sql & "INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(@id, @pc, @idDate);"
cmd.ActiveConnection = theDatabase
cmd.CommandType = adCmdText
cmd.CommandText = sql
cmd.Prepared = true
cmd.Parameters.Append cmd.CreateParameter("@id", adInteger, adParamInput, , id)
cmd.Parameters.Append cmd.CreateParameter("@pc", adVarchar, adParamInput, 12, pc)
cmd.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, -1, now)
cmd.Execute , , adExecuteNoRecords
set cmd = Nothing
end if
谢谢大家。
答案 0 :(得分:2)
在尝试设置命名参数值之前,使用Parameters
集合的Refresh()
method遗漏的上述代码没有任何问题。
Set cmd = server.createobject("ADODB.Command")
With
.ActiveConnection = theDatabase
.CommandType = adCmdStoredProc
.commandText = "AddResponse"
'Query the provider for the parameter details
Call .Parameters.Refresh()
.Parameters("@id") = id
.Parameters("@pc") = pc
.Parameters("@idDate") = now
Call .Execute(, , adExecuteNoRecords)
End With
set cmd = Nothing
如果您不想使用此方法,则参数定义必须来自某个地方,因此另一个选项是自己定义它们以反映存储过程的定义。
Set cmd = server.createobject("ADODB.Command")
With cmd
.ActiveConnection = theDatabase
.CommandType = adCmdStoredProc
.commandText = "AddResponse"
'Define parameters manually
Call .Parameters.Append(.CreateParameter("@id", adVarWChar, adParamInput, 12))
Call .Parameters.Append(.CreateParameter("@pc", adVarWChar, adParamInput, 12))
Call .Parameters.Append(.CreateParameter("@idDate", adDBTimeStamp, adParamInput, 8))
.Parameters("@id") = id
.Parameters("@pc") = pc
.Parameters("@idDate") = now
Call .Execute(, , adExecuteNoRecords)
End With
set cmd = Nothing
如果您确实沿着手动路线走下去,那么确定要使用的ADO DataTypeEnum常数是Carl Prothman - Data Type Mapping
旁注:您的存储过程中有此行;
select * from EmailResponse
哪些人希望返回结果集,但您在
adExecuteNoRecords
ADODB.Command
方法中指定Execute()
会导致忽略此项,如果您确实想要返回该结果集,请将上述内容调整为;Dim rs ... With cmd ... Set rs = .Execute() End With
...
用于显示省略代码的位置
需要指出,虽然@dimason approach (因为删除了,不知道为什么......)是合理的,但是当它们添加两个额外的参数时它会使事情复杂化不需要,你可以在动态SQL中声明参数,并指定它们使用那些本地声明的变量来代替运行语句。
Dim sql
sql = ""
sql = sql & "SET NOCOUNT ON;" & vbCrLf
sql = sql & "DECLARE @id NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @pc NVARCHAR(12)" & vbCrLf
sql = sql & "DECLARE @idDate DATETIME" & vbCrLf
sql = sql & "SELECT @id = ?, @pc = ?, @idDate = ?" & vbCrLf
sql = sql & "SELECT * FROM EmailResponse;" & vbCrLf
sql = sql & "IF NOT EXISTS (SELECT id, projectCode FROM EmailResponse WHERE id = @id and projectCode = @pc)" & vbCrLf
sql = sql & "INSERT INTO EmailResponse (id, projectCode, dateEntered) VALUEs(@id, @pc, @idDate);"
Set cmd = server.createobject("ADODB.Command")
With cmd
.ActiveConnection = theDatabase
.CommandType = adCmdText
.CommandText = sql
.Prepared = true
.Parameters.Append cmd.CreateParameter("@id", adVarChar, adParamInput, 12, id)
.Parameters.Append cmd.CreateParameter("@pc", adVarChar, adParamInput, 12, pc)
.Parameters.Append cmd.CreateParameter("@idDate", adDBTimeStamp, adParamInput, 8, Now())
Set rsOut = .Execute()
End With
Set cmd = Nothing
答案 1 :(得分:1)
要从存储过程切换到内联SQL,您需要更改
cmd.CommandType = adCmdStoredProc
到
cmd.CommandType = adCmdText
然后,您需要将查询添加到命令文本属性:
cmd.CommandText = "SELECT * FROM Orders WHERE CustomerID = ?"
以上行来自MSDN上的Command Object Parameters example。