VB6 ADO Command for SQL Server

I am getting an unexplained error with an ADO command in VB6 running with a SQL Server 2005 database.

Here's some code to demonstrate the problem:

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

      

So this code worked not too long ago, but now it doesn't work on the last line with an error:

Run-time error '3704': Operation is not allowed when the object is closed

      

Why is it closed? I just opened it and SP returns strings.

I ran a trace and this is what the ADO library actually sends to the server:

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

      

Doing this as a separate batch from my query editor gives:

Msg 102, Level 15, State 1, Line 4
Incorrect syntax near '2010'.

      

Of course there is a mistake. Take a look at the double single quotes. What the hell could be causing this? I tried using adDBDate and adDBTime as datatypes for the date parameter and they give the same results.

When I do the parameters to adParamInputOutput I get this:

declare @p1 int
set @p1=default
declare @p2 datetime
set @p2=default
exec ErrorAlertCollect @ErrorAlertID=@p1 output,@CreateTime=@p2 output
select @p1, @p2

      

Doing this as a separate batch gives:

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'.

      

What the hell? SQL Server does not support this syntax. The DEFAULT keyword can only be used in an actual SP statement.

I have to point out that removing the extra single quotes from the above description makes the SP perform fine.

... Oh, my. I just figured it out. I suppose it's worth posting anyway.

+2


a source to share


2 answers


The answer is that the stored procedure needed the SET NOCOUNT ON

top because ADO stops the moment when the response "Rows Affected" is returned, and the stored procedure has an update statement before making a final selection.



I don't know why the trace shows the ADODB syntax not working correctly when submitted on its own, but obviously the ADODB library was NOT getting an error with its requests. The whole problem was SET NOCOUNT ON

missing.

+2


a source


In addition to what @ErikE mentioned, if your sproc contains PRINT instructions (even if left for debugging), ADO will also have a cow, creating the exact same exact error that you see.



+2


a source







All Articles