views:

30

answers:

1

If the database is not Oracle, it is MS SQl 2008. My task: if Oracle, add two more parameters when calling a stored proc.

Oracle and MSFT stored procs are generated; Oracle ones have 3 extra parameters:

Vret_val out number,
Vparam2 in out number,
Vparam3 in out number,
... the rest

(The are not actually named Vparam2 and Vparam3, but this should not matter). So, the code for a helper VB.Net class that calls a stored proc:

Imports System.Data.Odbc
Imports System.Configuration

Dim objCon As OdbcConnection = Nothing
Dim objAdapter As OdbcDataAdapter
Dim cmdCommand As New OdbcCommand
Dim objDataTable As DataTable

Dim sconnection As String

Try
    sconnection = mConnectionString
    objAdapter = New OdbcDataAdapter
    objCon = New OdbcConnection(sconnection)
    objCon.Open()

    objAdapter.SelectCommand = cmdCommand
    objAdapter.SelectCommand.Connection = objCon
    objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

    objAdapter.SelectCommand.CommandTimeout = Globals.mReportTimeOut

    If Not mIsOracle Then
        objAdapter.SelectCommand.CommandText = String.Format("{{call {0}}}", spName)
    Else
        Dim returnValue As New OdbcParameter
        returnValue.Direction = ParameterDirection.Output
        returnValue.ParameterName = "@Vret_val"
        returnValue.OdbcType = OdbcType.Numeric
        objAdapter.SelectCommand.Parameters.Add(returnValue)

        objAdapter.SelectCommand.CommandText = String.Format("{{call {0}(?)}}", spName)
    End If

    Try

        objDataTable = New DataTable(spName)
        objAdapter.Fill(objDataTable)

    Catch ex As Exception
    ...

Question: I am puzzled as to what String.Format("{{call {0}(?)}}", spName) does, in particular the (?) part. My understanding of the String.Format is that it will simply replace {0} with spName. The {{, }}, and (?) do throw me off because { reminds me of formatting, (?) hints at some advanced regex use.

Unfortunately I am getting little help from a key person who is on vacation without a leash [smart]phone.

I am guessing that I simply add 5 more lines for each additional parameter, and change String.Format("{{call {0}(?)}}", spName) to String.Format("{{call {0}(?,?,?)}}", spName). I forgot to mention that I am coding this "blindly" - I have a compiler to help me, but no environment set up to test this.

This will be over in a few days, but I need to do my best to try finishing it on time :)

Thanks.

+1  A: 

String.Format("{{call {0}(?)}}", spName) will produce a string like this: "{call ProcName(?)}" (assuming spName contains the string ProcName). {{ and ´}}are needed if you want the to include{or}` in the string without them being part of the formatting.

This is mentioned in the documentation for string.Format:

The leading and trailing brace characters, '{' and '}', are required. To specify a single literal brace character in format, specify two leading or trailing brace characters; that is, "{{" or "}}".

The (?) means nothing special for the string.Format function, but will be used when executing the given procedure, each question mark representing a parameter that should be passed to the procedure.

Fredrik Mörk
Thanks ... but what happens to the `(?)` ?
Hamish Grubijan
@Hamish: I updated the answer.
Fredrik Mörk
Thanks again ... now, about meaning of `(?)` - was that a guess. I have to add 2 more, so should it be `(???)`, `(?,?,?)`, `(?)(?)(?)`, `(?),(?),(?)`, or something else? This follow-up question is open to everyone.
Hamish Grubijan
Without having tested it, I would guess the second option: `(?,?,?)`.
Fredrik Mörk