Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I associate Parameters to Command objects in ADO with VBScript?

Tags:

vbscript

ado

I have been working an ADO VBScript that needs to accept parameters and incorporate those parameters in the Query string that gets passed the the database. I keep getting errors when the Record Set Object attempts to open. If I pass a query without parameters, the recordset opens and I can work with the data. When I run the script through a debugger, the command object does not show a value for the parameter object. It seems to me that I am missing something that associates the Command object and Parameter object, but I do not know what. Here is a bit of the VBScript Code:

...
'Open Text file to collect SQL query string'
Set fso = CreateObject("Scripting.FileSystemObject")
fileName = "C:\SQLFUN\Limits_ADO.sql"
Set tso = fso.OpenTextFile(fileName, FORREADING)

SQL = tso.ReadAll

'Create ADO instance'
 connString = "DRIVER={SQL Server};SERVER=myserver;UID=MyName;PWD=notapassword;   Database=favoriteDB"
 Set connection = CreateObject("ADODB.Connection")
 Set cmd = CreateObject("ADODB.Command")

  connection.Open connString
  cmd.ActiveConnection = connection
  cmd.CommandText = SQL
  cmd.CommandType = adCmdText

  Set paramTotals = cmd.CreateParameter
  With paramTotals
       .value = "tot%"
       .Name = "Param1"
  End With

  'The error occurs on the next line'
  Set recordset = cmd.Execute

  If recordset.EOF then
      WScript.Echo "No Data Returned"
  Else
      Do Until recordset.EOF
            WScript.Echo recordset.Fields.Item(0) ' & vbTab & recordset.Fields.Item(1)
            recordset.MoveNext
      Loop
  End If

The SQL string that I use is fairly standard except I want to pass a parameter to it. It is something like this:

SELECT column1
FROM table1
WHERE column1 IS LIKE ?

I understand that ADO should replace the "?" with the parameter value I assign in the script. The problem I am seeing is that the Parameter object shows the correct value, but the command object's parameter field is null according to my debugger.

like image 526
Krashman5k Avatar asked Apr 01 '10 01:04

Krashman5k


People also ask

What is the use of parameters in command object?

A Command object has a Parameters collection made up of Parameter objects. Using the Refresh method on a Command object's Parameters collection retrieves provider parameter information for the stored procedure or parameterized query specified in the Command object.

What are the usages of the command object in Ado net?

The ADO Command object is used to execute a single query against a database. The query can perform actions like creating, adding, retrieving, deleting or updating records. If the query is used to retrieve data, the data will be returned as a RecordSet object.

Which method of command object returns an object?

The ExecuteScalar Method in SqlCommandObject returns the first column of the first row after executing the query against the Data Source. If the result set contains more than one column or rows, it takes only the first column of the first row. All other values are ignored.


2 Answers

I know this is old, but for anyone still fiding this (like I did via google):

If you're using Stored Procedures:

set cmd = Server.CreateObject("ADODB.Command")
with cmd
    .ActiveConnection = db_connection
    .CommandText = "stored_procedure_name"
    .CommandType = adCmdStoredProc
    .Parameters.Append .CreateParameter("@Parameter1",adInteger,adParamInput,,1)
        .Parameters.Append .CreateParameter("@Parameter2",adVarChar,adParamInput,100,"Up to 100 chars")
        .Parameters.Append .CreateParameter("@Parameter3",adBoolean,adParamInput,,true)
        .Parameters.Append .CreateParameter("@Parameter4",adDBTimeStamp,adParamInput,,now())
end with
set rs = cmd.execute
    'do stuff with returned results from select or leave blank if insert/delete/etc stored procedure
set rs = nothing
set cmd = nothing

If not, I beleive you change the .CommandText to your SQL statement with questions marks in place and your Parameters must follow the same order.

See http://www.devguru.com/technologies/ado/quickref/command_createparameter.html For a breakdown of what values you're passing with CreateParameter, as well as a list of types and their descriptions.

like image 56
Bradley Avatar answered Oct 11 '22 08:10

Bradley


After you create the parameter, you have to append it to the Command object's Parameters collection before you execute the Command:

Set paramTotals = cmd.CreateParameter
With paramTotals
    .Value = "tot%"
    .Name = "Param1"
End With

cmd.Parameters.Append paramTotals

You may also need to specify the Type and Size properties for the Parameter. Generally, I use the arguments of the CreateParameter function to set all the necessary properties in one line:

Set paramTotals = cmd.CreateParameter("Param1", adVarChar, adParamInput, 30, "tot%")
cmd.Parameters.Append paramTotals
like image 28
Cheran Shunmugavel Avatar answered Oct 11 '22 09:10

Cheran Shunmugavel