请尝试一下这个 VB.Net Oracle 相关示例并帮助我处理 String.Format

发布于 2024-08-27 08:41:10 字数 2091 浏览 7 评论 0原文

如果数据库不是 Oracle,则为 MS SQl 2008。 我的任务:如果是Oracle,在调用存储过程时再添加两个参数。

生成Oracle和MSFT存储过程; Oracle 有 3 个额外参数:(

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

它们实际上并未命名为 Vparam2 和 Vparam3,但这应该不重要)。 因此,调用存储过程的辅助 VB.Net 类的代码:

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

问题:我很困惑 String.Format("{{call {0}(?)}}", spName) 确实如此,特别是 (?) 部分。我对 String.Format 的理解是它会简单地将 {0} 替换为 spName。 {{}}(?) 确实让我感到困惑,因为 { 提醒我格式化, (?) 暗示了一些高级正则表达式的使用。

不幸的是,我从一位正在度假且没有皮带[智能]手机的关键人物那里得到了很少的帮助。

我猜我只是为每个附加参数添加 5 行,并将 String.Format("{{call {0}(?)}}", spName) 更改为 String。格式("{{call {0}(?,?,?)}}", spName).我忘了提到我正在“盲目地”编码 - 我有一个编译器来帮助我,但没有设置环境来测试它。

这将在几天内结束,但我需要尽力按时完成它:)

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新人笑 2024-09-03 08:41:11

String.Format("{{call {0}(?)}}", spName) 将生成如下字符串: "{call ProcName(?)}" (假设 spName 包含字符串 ProcName)。如果您希望在字符串中包含{}`而不将它们作为格式的一部分,则需要 {{ 和 ´}}

string.Format< 的文档中提到了这一点/a>:

前大括号和尾大括号
字符“{”和“}”是必需的。
指定单个文字大括号
格式中的字符,指定两个
前导或尾随大括号字符;
即“{{”或“}}”。

(?) 对于 string.Format 函数没有什么特别的意义,但在执行给定过程时会使用,每个问号代表一个应该传递给程序。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文