【发布时间】:2010-04-22 23:11:39
【问题描述】:
我在 VB6 中针对 SQL Server 2005 数据库运行 ADO 命令时遇到莫名其妙的错误。
这里有一些代码来演示这个问题:
Sub ADOCommand()
Dim Conn As ADODB.Connection
Dim Rs As ADODB.Recordset
Dim Cmd As ADODB.Command
Dim ErrorAlertID As Long
Dim ErrorTime As Date
Set Conn = New ADODB.Connection
Conn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Initial Catalog=database;Data Source=server"
Conn.CursorLocation = adUseClient
Conn.Open
Set Rs = New ADODB.Recordset
Rs.CursorType = adOpenStatic
Rs.LockType = adLockReadOnly
Set Cmd = New ADODB.Command
With Cmd
.Prepared = False
.CommandText = "ErrorAlertCollect"
.CommandType = adCmdStoredProc
.NamedParameters = True
.Parameters.Append .CreateParameter("@ErrorAlertID", adInteger, adParamOutput)
.Parameters.Append .CreateParameter("@CreateTime", adDate, adParamOutput)
Set .ActiveConnection = Conn
Rs.Open Cmd
ErrorAlertID = .Parameters("@ErrorAlertID").Value
ErrorTime = .Parameters("@CreateTime").Value
End With
Debug.Print Rs.State ''// Shows 0 - Closed
Debug.Print Rs.RecordCount ''// Of course this fails since the recordset is closed
End Sub
所以这段代码不久前还在工作,但现在它在最后一行失败并出现错误:
Run-time error '3704': Operation is not allowed when the object is closed
为什么关门了?我刚打开它,SP返回行。
我运行了一个跟踪,这就是 ADO 库实际提交给服务器的内容:
declare @p1 int
set @p1=1
declare @p2 datetime
set @p2=''2010-04-22 15:31:07:770''
exec ErrorAlertCollect @ErrorAlertID=@p1 output,@CreateTime=@p2 output
select @p1, @p2
从我的查询编辑器中将其作为单独的批处理运行会产生:
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '2010'.
当然有错误。看看那里的双单引号。到底是什么原因造成的?我尝试使用 adDBDate 和 adDBTime 作为日期参数的数据类型,它们给出了相同的结果。
当我设置参数 adParamInputOutput 时,我得到了这个:
declare @p1 int
set @p1=default
declare @p2 datetime
set @p2=default
exec ErrorAlertCollect @ErrorAlertID=@p1 output,@CreateTime=@p2 output
select @p1, @p2
将其作为单独的批次运行:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'default'.
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'default'.
什么鬼? SQL Server 不支持这种语法。在实际的 SP 执行语句中只能使用 DEFAULT 关键字。
我应该注意,从上面的语句中删除多余的单引号会使 SP 运行良好。
...哦,天哪。我刚刚想通了。我想无论如何都值得发布。
【问题讨论】:
-
''datetime''只是分析器中的一个已知错误(我认为在 SP2 中已修复) -
谢谢。奇怪的是,探查器会报告与真实情况不同的东西。我将检查服务器的 Service Pack 级别。
标签: sql-server vb6 command ado