开发者

Please take a stab at this VB.Net Oracle-related sample and help me with String.Format

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 Odb开发者_如何转开发cDataAdapter
    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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜