请尝试一下这个 VB.Net Oracle 相关示例并帮助我处理 String.Format
如果数据库不是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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(?)}"
(assumingspName
contains the stringProcName
).{{
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
(?)
means nothing special for thestring.Format
function, but will be used when executing the given procedure, each question mark representing a parameter that should be passed to the procedure.